It is possible to validate the request before a data object is constructed. This can be done by adding validation attributes to the properties of a data object like this:
class SongData extends Data
{
public function __construct(
#[Uuid()]
public string $uuid,
#[Max(15), IP, StartsWith('192.')]
public string $ip,
) {
}
}
##Creating your validation attribute
It is possible to create your own validation attribute by extending the CustomValidationAttribute
class, this class has a getRules
method that returns the rules that should be applied to the property.
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class CustomRule extends CustomValidationAttribute
{
public function getRules(ValidationPath $path): array|object|string;
{
return [new CustomRule()];
}
}
Quick note: you can only use these rules as an attribute, not as a class rule within the static rules
method of the data class.
##Available validation attributes
##Accepted
Docs
#[Accepted]
public bool $closure;
##AcceptedIf
Docs
#[AcceptedIf('other_field', 'equals_this')]
public bool $closure;
##ActiveUrl
Docs
#[ActiveUrl]
public string $closure;
##After
Docs
#[After('tomorrow')]
public Carbon $closure;
#[After(Carbon::yesterday())]
public Carbon $closure;
#[After(new FieldReference('other_field'))]
public Carbon $closure;
##AfterOrEqual
Docs
#[AfterOrEqual('tomorrow')]
public Carbon $closure;
#[AfterOrEqual(Carbon::yesterday())]
public Carbon $closure;
#[AfterOrEqual(new FieldReference('other_field'))]
public Carbon $closure;
##Alpha
Docs
#[Alpha]
public string $closure;
##AlphaDash
Docs
#[AlphaDash]
public string $closure;
##AlphaNumeric
Docs
#[AlphaNumeric]
public string $closure;
##ArrayType
Docs
#[ArrayType]
public array $closure;
#[ArrayType(['valid_key', 'other_valid_key'])]
public array $closure;
#[ArrayType('valid_key', 'other_valid_key')]
public array $closure;
Docs
#[Bail]
public string $closure;
##Before
Docs
#[Before('tomorrow')]
public Carbon $closure;
#[Before(Carbon::yesterday())]
public Carbon $closure;
#[Before(new FieldReference('other_field'))]
public Carbon $closure;
##BeforeOrEqual
Docs
#[BeforeOrEqual('tomorrow')]
public Carbon $closure;
#[BeforeOrEqual(Carbon::yesterday())]
public Carbon $closure;
#[BeforeOrEqual(new FieldReference('other_field'))]
public Carbon $closure;
##Between
Docs
#[Between(3.14, 42)]
public int $closure;
##BooleanType
Docs
#[BooleanType]
public bool $closure;
##Confirmed
Docs
#[Confirmed]
public string $closure;
##CurrentPassword
Docs
#[CurrentPassword]
public string $closure;
#[CurrentPassword('api')]
public string $closure;
Docs
#[Date]
public Carbon $closure;
##DateEquals
Docs
#[DateEquals('tomorrow')]
public Carbon $closure;
#[DateEquals(Carbon::yesterday())]
public Carbon $closure;
##DateFormat
Docs
#[DateFormat('d-m-Y')]
public Carbon $closure;
##Declined
Docs
#[Declined]
public bool $closure;
##DeclinedIf
Docs
#[DeclinedIf('other_field', 'equals_this')]
public bool $closure;
##Different
Docs
#[Different('other_field')]
public string $closure;
##Digits
Docs
#[Digits(10)]
public int $closure;
##DigitsBetween
Docs
#[DigitsBetween(2, 10)]
public int $closure;
##Dimensions
Docs
#[Dimensions(ratio: 1.5)]
public UploadedFile $closure;
#[Dimensions(maxWidth: 100, maxHeight: 100)]
public UploadedFile $closure;
##Distinct
Docs
#[Distinct]
public string $closure;
#[Distinct(Distinct::Strict)]
public string $closure;
#[Distinct(Distinct::IgnoreCase)]
public string $closure;
##DoesntEndWith
Docs
#[DoesntEndWith('a')]
public string $closure;
#[DoesntEndWith(['a', 'b'])]
public string $closure;
#[DoesntEndWith('a', 'b')]
public string $closure;
##DoesntStartWith
Docs
#[DoesntStartWith('a')]
public string $closure;
#[DoesntStartWith(['a', 'b'])]
public string $closure;
#[DoesntStartWith('a', 'b')]
public string $closure;
##Email
Docs
#[Email]
public string $closure;
#[Email(Email::RfcValidation)]
public string $closure;
#[Email([Email::RfcValidation, Email::DnsCheckValidation])]
public string $closure;
#[Email(Email::RfcValidation, Email::DnsCheckValidation)]
public string $closure;
##EndsWith
Docs
#[EndsWith('a')]
public string $closure;
#[EndsWith(['a', 'b'])]
public string $closure;
#[EndsWith('a', 'b')]
public string $closure;
Docs
#[Enum(ChannelType::class)]
public string $closure;
##ExcludeIf
At the moment the data is not yet excluded due to technical reasons, v4 should fix this
Docs
#[ExcludeIf('other_field', 'has_value')]
public string $closure;
##ExcludeUnless
At the moment the data is not yet excluded due to technical reasons, v4 should fix this
Docs
#[ExcludeUnless('other_field', 'has_value')]
public string $closure;
##ExcludeWith
At the moment the data is not yet excluded due to technical reasons, v4 should fix this
Docs
#[ExcludeWith('other_field')]
public string $closure;
##ExcludeWithout
At the moment the data is not yet excluded due to technical reasons, v4 should fix this
Docs
#[ExcludeWithout('other_field')]
public string $closure;
##Exists
Docs
#[Exists('users')]
public string $closure;
#[Exists(User::class)]
public string $closure;
#[Exists('users', 'email')]
public string $closure;
#[Exists('users', 'email', connection: 'tenant')]
public string $closure;
#[Exists('users', 'email', withoutTrashed: true)]
public string $closure;
Docs
#[File]
public UploadedFile $closure;
##Filled
Docs
#[Filled]
public string $closure;
##GreaterThan
Docs
#[GreaterThan('other_field')]
public int $closure;
##GreaterThanOrEqualTo
Docs
#[GreaterThanOrEqualTo('other_field')]
public int $closure;
##Image
Docs
#[Image]
public UploadedFile $closure;
Docs
#[In([1, 2, 3, 'a', 'b'])]
public mixed $closure;
#[In(1, 2, 3, 'a', 'b')]
public mixed $closure;
##InArray
Docs
#[InArray('other_field')]
public string $closure;
##IntegerType
Docs
#[IntegerType]
public int $closure;
Docs
#[IP]
public string $closure;
Docs
#[IPv4]
public string $closure;
Docs
#[IPv6]
public string $closure;
Docs
#[Json]
public string $closure;
##LessThan
Docs
#[LessThan('other_field')]
public int $closure;
##LessThanOrEqualTo
Docs
#[LessThanOrEqualTo('other_field')]
public int $closure;
##Lowercase
Docs
#[Lowercase]
public string $closure;
##MacAddress
Docs
#[MacAddress]
public string $closure;
Docs
#[Max(20)]
public int $closure;
##MaxDigits
Docs
#[MaxDigits(10)]
public int $closure;
##MimeTypes
Docs
#[MimeTypes('video/quicktime')]
public UploadedFile $closure;
#[MimeTypes(['video/quicktime', 'video/avi'])]
public UploadedFile $closure;
#[MimeTypes('video/quicktime', 'video/avi')]
public UploadedFile $closure;
##Mimes
Docs
#[Mimes('jpg')]
public UploadedFile $closure;
#[Mimes(['jpg', 'png'])]
public UploadedFile $closure;
#[Mimes('jpg', 'png')]
public UploadedFile $closure;
Docs
#[Min(20)]
public int $closure;
##MinDigits
Docs
#[MinDigits(2)]
public int $closure;
##MultipleOf
Docs
#[MultipleOf(3)]
public int $closure;
##NotIn
Docs
#[NotIn([1, 2, 3, 'a', 'b'])]
public mixed $closure;
#[NotIn(1, 2, 3, 'a', 'b')]
public mixed $closure;
##NotRegex
Docs
#[NotRegex('/^.+$/i')]
public string $closure;
##Nullable
Docs
#[Nullable]
public ?string $closure;
##Numeric
Docs
#[Numeric]
public ?string $closure;
##Password
Docs
#[Password(min: 12, letters: true, mixedCase: true, numbers: false, symbols: false, uncompromised: true, uncompromisedThreshold: 0)]
public string $closure;
##Present
Docs
#[Present]
public string $closure;
##Prohibited
Docs
#[Prohibited]
public ?string $closure;
##ProhibitedIf
Docs
#[ProhibitedIf('other_field', 'has_value')]
public ?string $closure;
#[ProhibitedIf('other_field', ['has_value', 'or_this_value'])]
public ?string $closure;
##ProhibitedUnless
Docs
#[ProhibitedUnless('other_field', 'has_value')]
public ?string $closure;
#[ProhibitedUnless('other_field', ['has_value', 'or_this_value'])]
public ?string $closure;
##Prohibits
Docs
#[Prohibits('other_field')]
public ?string $closure;
#[Prohibits(['other_field', 'another_field'])]
public ?string $closure;
#[Prohibits('other_field', 'another_field')]
public ?string $closure;
##Regex
Docs
#[Regex('/^.+$/i')]
public string $closure;
##Required
Docs
#[Required]
public string $closure;
##RequiredIf
Docs
#[RequiredIf('other_field', 'value')]
public ?string $closure;
#[RequiredIf('other_field', ['value', 'another_value'])]
public ?string $closure;
##RequiredUnless
Docs
#[RequiredUnless('other_field', 'value')]
public ?string $closure;
#[RequiredUnless('other_field', ['value', 'another_value'])]
public ?string $closure;
##RequiredWith
Docs
#[RequiredWith('other_field')]
public ?string $closure;
#[RequiredWith(['other_field', 'another_field'])]
public ?string $closure;
#[RequiredWith('other_field', 'another_field')]
public ?string $closure;
##RequiredWithAll
Docs
#[RequiredWithAll('other_field')]
public ?string $closure;
#[RequiredWithAll(['other_field', 'another_field'])]
public ?string $closure;
#[RequiredWithAll('other_field', 'another_field')]
public ?string $closure;
##RequiredWithout
Docs
#[RequiredWithout('other_field')]
public ?string $closure;
#[RequiredWithout(['other_field', 'another_field'])]
public ?string $closure;
#[RequiredWithout('other_field', 'another_field')]
public ?string $closure;
##RequiredWithoutAll
Docs
#[RequiredWithoutAll('other_field')]
public ?string $closure;
#[RequiredWithoutAll(['other_field', 'another_field'])]
public ?string $closure;
#[RequiredWithoutAll('other_field', 'another_field')]
public ?string $closure;
##RequiredArrayKeys
Docs
#[RequiredArrayKeys('a')]
public array $closure;
#[RequiredArrayKeys(['a', 'b'])]
public array $closure;
#[RequiredArrayKeys('a', 'b')]
public array $closure;
#[Rule('string|uuid')]
public string $closure;
#[Rule(['string','uuid'])]
public string $closure;
Docs
#[Same('other_field')]
public string $closure;
Docs
#[Size(10)]
public string $closure;
##Sometimes
Docs
#[Sometimes]
public string $closure;
##StartsWith
Docs
#[StartsWith('a')]
public string $closure;
#[StartsWith(['a', 'b'])]
public string $closure;
#[StartsWith('a', 'b')]
public string $closure;
##StringType
Docs
#[StringType()]
public string $closure;
##TimeZone
Docs
#[TimeZone()]
public string $closure;
##Unique
Docs
#[Unique('users')]
public string $closure;
#[Unique(User::class)]
public string $closure;
#[Unique('users', 'email')]
public string $closure;
#[Unique('users', connection: 'tenant')]
public string $closure;
#[Unique('users', withoutTrashed: true)]
public string $closure;
#[Unique('users', ignore: 5)]
public string $closure;
##Uppercase
Docs
#[Uppercase]
public string $closure;
Docs
#[Url]
public string $closure;
Docs
#[Ulid]
public string $closure;
Docs
#[Uuid]
public string $closure;