generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 261
[feat] Add database constraints for validation annotations #1085
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Klaas058
wants to merge
5
commits into
spatie:main
Choose a base branch
from
Klaas058:feature/annotation-validation-database-constraints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b6c2922
[feat] Add database constraints for validation annotations
Klaas058 bbc3d7d
Merge branch 'main' of https://github.com/spatie/laravel-data into fe…
Klaas058 c74e2c4
[feat] Allow use of ExternalReference in database constraints & add t…
Klaas058 6892a66
[feat] Add AppliesDatabaseConstraints trait & add list of database co…
Klaas058 1d380d3
[feat] Use mixed for value types for where and whereNot
Klaas058 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php | ||
|
|
||
| namespace Spatie\LaravelData\Attributes\Concerns; | ||
|
|
||
| use Closure; | ||
| use InvalidArgumentException; | ||
| use Spatie\LaravelData\Support\Validation\Constraints\DatabaseConstraint; | ||
| use Spatie\LaravelData\Support\Validation\Constraints\WhereConstraint; | ||
| use Spatie\LaravelData\Support\Validation\Constraints\WhereInConstraint; | ||
| use Spatie\LaravelData\Support\Validation\Constraints\WhereNotConstraint; | ||
| use Spatie\LaravelData\Support\Validation\Constraints\WhereNotInConstraint; | ||
| use Spatie\LaravelData\Support\Validation\Constraints\WhereNotNullConstraint; | ||
| use Spatie\LaravelData\Support\Validation\Constraints\WhereNullConstraint; | ||
|
|
||
| trait AppliesDatabaseConstraints | ||
| { | ||
| /** | ||
| * @param object $rule A validation rule that uses the DatabaseRule trait (e.g., Unique, Exists) | ||
| */ | ||
| protected function applyDatabaseConstraints(object $rule, Closure|DatabaseConstraint|array $constraints): void | ||
| { | ||
| $constraintsList = is_array($constraints) ? $constraints : [$constraints]; | ||
|
|
||
| foreach ($constraintsList as $constraint) { | ||
| match (true) { | ||
| $constraint instanceof Closure => $rule->where($constraint), | ||
| $constraint instanceof WhereConstraint => $rule->where(...$constraint->toArray()), | ||
| $constraint instanceof WhereNotConstraint => $rule->whereNot(...$constraint->toArray()), | ||
| $constraint instanceof WhereNullConstraint => $rule->whereNull(...$constraint->toArray()), | ||
| $constraint instanceof WhereNotNullConstraint => $rule->whereNotNull(...$constraint->toArray()), | ||
| $constraint instanceof WhereInConstraint => $rule->whereIn(...$constraint->toArray()), | ||
| $constraint instanceof WhereNotInConstraint => $rule->whereNotIn(...$constraint->toArray()), | ||
| default => throw new InvalidArgumentException('Each where item must be a DatabaseConstraint or Closure'), | ||
| }; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| namespace Spatie\LaravelData\Attributes\Concerns; | ||
|
|
||
| use Spatie\LaravelData\Support\Validation\References\ExternalReference; | ||
|
|
||
| trait NormalizesExternalReferences | ||
| { | ||
| protected function normalizePossibleExternalReferenceParameter(mixed $parameter): mixed | ||
| { | ||
| if ($parameter instanceof ExternalReference) { | ||
| return $parameter->getValue(); | ||
| } | ||
|
|
||
| return $parameter; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php | ||
|
|
||
| namespace Spatie\LaravelData\Support\Validation\Constraints; | ||
|
|
||
| use Illuminate\Contracts\Support\Arrayable; | ||
| use Spatie\LaravelData\Attributes\Concerns\NormalizesExternalReferences; | ||
|
|
||
| abstract class DatabaseConstraint implements Arrayable | ||
| { | ||
| use NormalizesExternalReferences; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| namespace Spatie\LaravelData\Support\Validation\Constraints; | ||
|
|
||
| use Closure; | ||
| use Spatie\LaravelData\Support\Validation\References\ExternalReference; | ||
|
|
||
| class WhereConstraint extends DatabaseConstraint | ||
| { | ||
| public function __construct( | ||
| public readonly Closure|string|ExternalReference $column, | ||
| public readonly mixed $value = null, | ||
| ) { | ||
| } | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| return [ | ||
| $this->normalizePossibleExternalReferenceParameter($this->column), | ||
| $this->normalizePossibleExternalReferenceParameter($this->value), | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| namespace Spatie\LaravelData\Support\Validation\Constraints; | ||
|
|
||
| use BackedEnum; | ||
| use Illuminate\Contracts\Support\Arrayable; | ||
| use Spatie\LaravelData\Support\Validation\References\ExternalReference; | ||
|
|
||
| class WhereInConstraint extends DatabaseConstraint | ||
| { | ||
| public function __construct( | ||
| public readonly string|ExternalReference $column, | ||
| public readonly Arrayable|BackedEnum|array|ExternalReference $values, | ||
| ) {} | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| return [ | ||
| $this->normalizePossibleExternalReferenceParameter($this->column), | ||
| $this->normalizePossibleExternalReferenceParameter($this->values), | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| namespace Spatie\LaravelData\Support\Validation\Constraints; | ||
|
|
||
| use Spatie\LaravelData\Support\Validation\References\ExternalReference; | ||
|
|
||
| class WhereNotConstraint extends DatabaseConstraint | ||
| { | ||
| public function __construct( | ||
| public readonly string|ExternalReference $column, | ||
| public readonly mixed $value, | ||
| ) {} | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| return [ | ||
| $this->normalizePossibleExternalReferenceParameter($this->column), | ||
| $this->normalizePossibleExternalReferenceParameter($this->value), | ||
| ]; | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/Support/Validation/Constraints/WhereNotInConstraint.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| namespace Spatie\LaravelData\Support\Validation\Constraints; | ||
|
|
||
| use BackedEnum; | ||
| use Illuminate\Contracts\Support\Arrayable; | ||
| use Spatie\LaravelData\Support\Validation\References\ExternalReference; | ||
|
|
||
| class WhereNotInConstraint extends DatabaseConstraint | ||
| { | ||
| public function __construct( | ||
| public readonly string|ExternalReference $column, | ||
| public readonly Arrayable|BackedEnum|array|ExternalReference $values, | ||
| ) {} | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| return [ | ||
| $this->normalizePossibleExternalReferenceParameter($this->column), | ||
| $this->normalizePossibleExternalReferenceParameter($this->values), | ||
| ]; | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/Support/Validation/Constraints/WhereNotNullConstraint.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| namespace Spatie\LaravelData\Support\Validation\Constraints; | ||
|
|
||
| use Spatie\LaravelData\Support\Validation\References\ExternalReference; | ||
|
|
||
| class WhereNotNullConstraint extends DatabaseConstraint | ||
| { | ||
| public function __construct( | ||
| public readonly string|ExternalReference $column, | ||
| ) {} | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| return [ | ||
| $this->normalizePossibleExternalReferenceParameter($this->column), | ||
| ]; | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/Support/Validation/Constraints/WhereNullConstraint.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| namespace Spatie\LaravelData\Support\Validation\Constraints; | ||
|
|
||
| use Spatie\LaravelData\Support\Validation\References\ExternalReference; | ||
|
|
||
| class WhereNullConstraint extends DatabaseConstraint | ||
| { | ||
| public function __construct( | ||
| public readonly string|ExternalReference $column, | ||
| ) {} | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| return [ | ||
| $this->normalizePossibleExternalReferenceParameter($this->column), | ||
| ]; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a list here with the possible constraints?