generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce various validation rule attributes
Added new validation rule attributes including ascii, decimal, gte (greater than or equal), lte (less than or equal), mime type, missing, and present. These attributes enhance the robustness and specificity of data validation across the application.
- Loading branch information
1 parent
0d6c883
commit df78be5
Showing
16 changed files
with
473 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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\Attributes\Validation; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)] | ||
class Ascii extends StringValidationAttribute | ||
{ | ||
public static function keyword(): string | ||
{ | ||
return 'ascii'; | ||
} | ||
|
||
public function parameters(): array | ||
{ | ||
return []; | ||
} | ||
} |
This file contains 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,24 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Attributes\Validation; | ||
|
||
use Attribute; | ||
use Spatie\LaravelData\Support\Validation\References\RouteParameterReference; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)] | ||
class Decimal extends StringValidationAttribute | ||
{ | ||
public function __construct(protected int|float|RouteParameterReference $min, protected null|int|float|RouteParameterReference $max) | ||
{ | ||
} | ||
|
||
public static function keyword(): string | ||
{ | ||
return 'decimal'; | ||
} | ||
|
||
public function parameters(): array | ||
{ | ||
return array_filter([$this->min, $this->max]); | ||
} | ||
} |
This file contains 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,29 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Attributes\Validation; | ||
|
||
use Attribute; | ||
use Spatie\LaravelData\Support\Validation\References\FieldReference; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)] | ||
class GreaterThanOrEqual extends StringValidationAttribute | ||
{ | ||
protected FieldReference $field; | ||
|
||
public function __construct( | ||
string|FieldReference $field, | ||
) { | ||
$this->field = $this->parseFieldReference($field); | ||
} | ||
|
||
|
||
public static function keyword(): string | ||
{ | ||
return 'gte'; | ||
} | ||
|
||
public function parameters(): array | ||
{ | ||
return [$this->field]; | ||
} | ||
} |
This file contains 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,29 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Attributes\Validation; | ||
|
||
use Attribute; | ||
use Spatie\LaravelData\Support\Validation\References\FieldReference; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)] | ||
class LessThanOrEqual extends StringValidationAttribute | ||
{ | ||
protected FieldReference $field; | ||
|
||
public function __construct( | ||
string|FieldReference $field, | ||
) { | ||
$this->field = $this->parseFieldReference($field); | ||
} | ||
|
||
|
||
public static function keyword(): string | ||
{ | ||
return 'lte'; | ||
} | ||
|
||
public function parameters(): array | ||
{ | ||
return [$this->field]; | ||
} | ||
} |
This file contains 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,25 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Attributes\Validation; | ||
|
||
use Attribute; | ||
use Illuminate\Support\Arr; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)] | ||
class MimeTypeByFileExtension extends StringValidationAttribute | ||
{ | ||
public function __construct( | ||
private readonly array|string $types, | ||
) { | ||
} | ||
|
||
public static function keyword(): string | ||
{ | ||
return 'mimes'; | ||
} | ||
|
||
public function parameters(): array | ||
{ | ||
return Arr::wrap($this->types); | ||
} | ||
} |
This file contains 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\Attributes\Validation; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)] | ||
class Missing extends StringValidationAttribute | ||
{ | ||
public static function keyword(): string | ||
{ | ||
return 'missing'; | ||
} | ||
|
||
public function parameters(): array | ||
{ | ||
return []; | ||
} | ||
} |
This file contains 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,39 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Attributes\Validation; | ||
|
||
use Attribute; | ||
use BackedEnum; | ||
use Illuminate\Support\Arr; | ||
use Spatie\LaravelData\Support\Validation\References\FieldReference; | ||
use Spatie\LaravelData\Support\Validation\References\RouteParameterReference; | ||
use Spatie\LaravelData\Support\Validation\RequiringRule; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)] | ||
class MissingIf extends StringValidationAttribute implements RequiringRule | ||
{ | ||
protected FieldReference $field; | ||
|
||
protected string|array $values; | ||
|
||
public function __construct( | ||
string|FieldReference $field, | ||
array|string|BackedEnum|RouteParameterReference ...$values | ||
) { | ||
$this->field = $this->parseFieldReference($field); | ||
$this->values = Arr::flatten($values); | ||
} | ||
|
||
public static function keyword(): string | ||
{ | ||
return 'missing_if'; | ||
} | ||
|
||
public function parameters(): array | ||
{ | ||
return [ | ||
$this->field, | ||
$this->values, | ||
]; | ||
} | ||
} |
This file contains 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,40 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Attributes\Validation; | ||
|
||
use Attribute; | ||
use BackedEnum; | ||
use Illuminate\Support\Arr; | ||
use Spatie\LaravelData\Support\Validation\References\FieldReference; | ||
use Spatie\LaravelData\Support\Validation\References\RouteParameterReference; | ||
use Spatie\LaravelData\Support\Validation\RequiringRule; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)] | ||
class MissingUnless extends StringValidationAttribute implements RequiringRule | ||
{ | ||
protected FieldReference $field; | ||
|
||
protected string|array $values; | ||
|
||
public function __construct( | ||
string|FieldReference $field, | ||
null|array|string|BackedEnum|RouteParameterReference ...$values | ||
) { | ||
$this->field = $this->parseFieldReference($field); | ||
$this->values = Arr::flatten($values); | ||
} | ||
|
||
|
||
public static function keyword(): string | ||
{ | ||
return 'missing_unless'; | ||
} | ||
|
||
public function parameters(): array | ||
{ | ||
return [ | ||
$this->field, | ||
$this->values, | ||
]; | ||
} | ||
} |
This file contains 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,33 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Attributes\Validation; | ||
|
||
use Attribute; | ||
use Illuminate\Support\Arr; | ||
use Spatie\LaravelData\Support\Validation\References\FieldReference; | ||
use Spatie\LaravelData\Support\Validation\RequiringRule; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)] | ||
class MissingWith extends StringValidationAttribute implements RequiringRule | ||
{ | ||
protected array $fields; | ||
|
||
public function __construct(array|string|FieldReference ...$fields) | ||
{ | ||
foreach (Arr::flatten($fields) as $field) { | ||
$this->fields[] = $field instanceof FieldReference ? $field : new FieldReference($field); | ||
} | ||
} | ||
|
||
public static function keyword(): string | ||
{ | ||
return 'missing_with'; | ||
} | ||
|
||
public function parameters(): array | ||
{ | ||
return [ | ||
$this->fields, | ||
]; | ||
} | ||
} |
This file contains 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,33 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Attributes\Validation; | ||
|
||
use Attribute; | ||
use Illuminate\Support\Arr; | ||
use Spatie\LaravelData\Support\Validation\References\FieldReference; | ||
use Spatie\LaravelData\Support\Validation\RequiringRule; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)] | ||
class MissingWithAll extends StringValidationAttribute implements RequiringRule | ||
{ | ||
protected array $fields; | ||
|
||
public function __construct(array|string|FieldReference ...$fields) | ||
{ | ||
foreach (Arr::flatten($fields) as $field) { | ||
$this->fields[] = $field instanceof FieldReference ? $field : new FieldReference($field); | ||
} | ||
} | ||
|
||
public static function keyword(): string | ||
{ | ||
return 'missing_with_all'; | ||
} | ||
|
||
public function parameters(): array | ||
{ | ||
return [ | ||
$this->fields, | ||
]; | ||
} | ||
} |
This file contains 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\Attributes\Validation; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)] | ||
class Negative extends StringValidationAttribute | ||
{ | ||
public static function keyword(): string | ||
{ | ||
return 'lt'; | ||
} | ||
|
||
public function parameters(): array | ||
{ | ||
return [0]; | ||
} | ||
} |
This file contains 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\Attributes\Validation; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)] | ||
class Positive extends StringValidationAttribute | ||
{ | ||
public static function keyword(): string | ||
{ | ||
return 'gt'; | ||
} | ||
|
||
public function parameters(): array | ||
{ | ||
return [0]; | ||
} | ||
} |
This file contains 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,39 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Attributes\Validation; | ||
|
||
use Attribute; | ||
use BackedEnum; | ||
use Illuminate\Support\Arr; | ||
use Spatie\LaravelData\Support\Validation\References\FieldReference; | ||
use Spatie\LaravelData\Support\Validation\References\RouteParameterReference; | ||
use Spatie\LaravelData\Support\Validation\RequiringRule; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)] | ||
class PresentIf extends StringValidationAttribute implements RequiringRule | ||
{ | ||
protected FieldReference $field; | ||
|
||
protected string|array $values; | ||
|
||
public function __construct( | ||
string|FieldReference $field, | ||
array|string|BackedEnum|RouteParameterReference ...$values | ||
) { | ||
$this->field = $this->parseFieldReference($field); | ||
$this->values = Arr::flatten($values); | ||
} | ||
|
||
public static function keyword(): string | ||
{ | ||
return 'present_if'; | ||
} | ||
|
||
public function parameters(): array | ||
{ | ||
return [ | ||
$this->field, | ||
$this->values, | ||
]; | ||
} | ||
} |
Oops, something went wrong.