-
-
Notifications
You must be signed in to change notification settings - Fork 257
[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
base: main
Are you sure you want to change the base?
Changes from 1 commit
b6c2922
bbc3d7d
c74e2c4
6892a66
1d380d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,8 +6,16 @@ | |
| use Closure; | ||
| use Exception; | ||
| use Illuminate\Validation\Rules\Exists as BaseExists; | ||
| use InvalidArgumentException; | ||
| use Spatie\LaravelData\Support\Validation\Constraints\WhereConstraint; | ||
| use Spatie\LaravelData\Support\Validation\Constraints\WhereNotConstraint; | ||
| use Spatie\LaravelData\Support\Validation\Constraints\WhereNullConstraint; | ||
| use Spatie\LaravelData\Support\Validation\Constraints\WhereNotNullConstraint; | ||
| use Spatie\LaravelData\Support\Validation\Constraints\WhereInConstraint; | ||
| use Spatie\LaravelData\Support\Validation\Constraints\WhereNotInConstraint; | ||
| use Spatie\LaravelData\Support\Validation\References\ExternalReference; | ||
| use Spatie\LaravelData\Support\Validation\ValidationPath; | ||
| use Spatie\LaravelData\Support\Validation\Constraints\DatabaseConstraint; | ||
|
|
||
| #[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)] | ||
| class Exists extends ObjectValidationAttribute | ||
|
|
@@ -18,7 +26,7 @@ public function __construct( | |
| protected null|string|ExternalReference $connection = null, | ||
| protected bool|ExternalReference $withoutTrashed = false, | ||
| protected string|ExternalReference $deletedAtColumn = 'deleted_at', | ||
| protected ?Closure $where = null, | ||
| protected null|Closure|DatabaseConstraint|array $where = null, | ||
| protected ?BaseExists $rule = null, | ||
| ) { | ||
| if ($rule === null && $table === null) { | ||
|
|
@@ -48,7 +56,20 @@ public function getRule(ValidationPath $path): object|string | |
| } | ||
|
|
||
| if ($this->where) { | ||
| $rule->where($this->where); | ||
| $constraints = is_array($this->where) ? $this->where : [$this->where]; | ||
|
|
||
| foreach ($constraints 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'), | ||
| }; | ||
| } | ||
|
||
| } | ||
|
|
||
| return $rule; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <?php | ||
|
|
||
| namespace Spatie\LaravelData\Support\Validation\Constraints; | ||
|
|
||
| use Illuminate\Contracts\Support\Arrayable; | ||
|
|
||
| interface DatabaseConstraint extends Arrayable | ||
| { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| namespace Spatie\LaravelData\Support\Validation\Constraints; | ||
|
|
||
| class WhereConstraint implements DatabaseConstraint | ||
| { | ||
| public function __construct( | ||
| public readonly mixed $column, | ||
| public readonly mixed $value = null, | ||
|
||
| ) { | ||
| } | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| return [$this->column, $this->value]; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| namespace Spatie\LaravelData\Support\Validation\Constraints; | ||
|
|
||
| class WhereInConstraint implements DatabaseConstraint | ||
| { | ||
| public function __construct( | ||
| public readonly mixed $column, | ||
| public readonly mixed $values, | ||
| ) {} | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| return [$this->column, $this->values]; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| namespace Spatie\LaravelData\Support\Validation\Constraints; | ||
|
|
||
| class WhereNotConstraint implements DatabaseConstraint | ||
| { | ||
| public function __construct( | ||
| public readonly mixed $column, | ||
| public readonly mixed $value, | ||
| ) {} | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| return [$this->column, $this->value]; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| namespace Spatie\LaravelData\Support\Validation\Constraints; | ||
|
|
||
| class WhereNotInConstraint implements DatabaseConstraint | ||
| { | ||
| public function __construct( | ||
| public readonly mixed $column, | ||
| public readonly mixed $values, | ||
| ) {} | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| return [$this->column, $this->values]; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
|
|
||
| namespace Spatie\LaravelData\Support\Validation\Constraints; | ||
|
|
||
| class WhereNotNullConstraint implements DatabaseConstraint | ||
| { | ||
| public function __construct( | ||
| public readonly mixed $column, | ||
| ) {} | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| return [$this->column]; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
|
|
||
| namespace Spatie\LaravelData\Support\Validation\Constraints; | ||
|
|
||
| class WhereNullConstraint implements DatabaseConstraint | ||
| { | ||
| public function __construct( | ||
| public readonly mixed $column, | ||
| ) {} | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| return [$this->column]; | ||
| } | ||
| } |
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?