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
A validation attribute is a class that extends ValidationAttribute
and returns an array of validation rules when the getRules
method is called:
#[Attribute(Attribute::TARGET_PROPERTY)]
class CustomRule extends ValidationAttribute
{
public function getRules(): array
{
return [new CustomRule()];
}
}
##Available validation attributes
##Accepted
Docs
#[Accepted]
public bool $value;
##AcceptedIf
Docs
#[AcceptedIf('other_field', 'equals_this')]
public bool $value;
##ActiveUrl
Docs
#[ActiveUrl]
public string $value;
##After
Docs
#[After('other_field')]
public Carbon $value;
#[After('tomorrow')]
public Carbon $value;
#[After(Carbon::yesterday())]
public Carbon $value;
##AfterOrEqual
Docs
#[AfterOrEqual('other_field')]
public Carbon $value;
#[AfterOrEqual('tomorrow')]
public Carbon $value;
#[AfterOrEqual(Carbon::yesterday())]
public Carbon $value;
##Alpha
Docs
#[Alpha]
public string $value;
##AlphaDash
Docs
#[AlphaDash]
public string $value;
##AlphaNumeric
Docs
#[AlphaNumeric]
public string $value;
##ArrayType
Docs
#[ArrayType]
public array $value;
#[ArrayType(['valid_key', 'other_valid_key'])]
public array $value;
#[ArrayType('valid_key', 'other_valid_key')]
public array $value;
Docs
#[Bail]
public string $value;
##Before
Docs
#[Before('other_field')]
public Carbon $value;
#[Before('tomorrow')]
public Carbon $value;
#[Before(Carbon::yesterday())]
public Carbon $value;
##BeforeOrEqual
Docs
#[BeforeOrEqual('other_field')]
public Carbon $value;
#[BeforeOrEqual('tomorrow')]
public Carbon $value;
#[BeforeOrEqual(Carbon::yesterday())]
public Carbon $value;
##Between
Docs
#[Between(3.14, 42)]
public int $value;
##BooleanType
Docs
#[BooleanType]
public bool $value;
##Confirmed
Docs
#[Confirmed]
public string $value;
##CurrentPassword
Docs
#[CurrentPassword]
public string $value;
#[CurrentPassword('api')]
public string $value;
Docs
#[Date]
public Carbon $value;
##DateEquals
Docs
#[DateEquals('tomorrow')]
public Carbon $value;
#[DateEquals(Carbon::yesterday())]
public Carbon $value;
##DateFormat
Docs
#[DateFormat('d-m-Y')]
public Carbon $value;
##Different
Docs
#[Different('other_field')]
public string $value;
##Digits
Docs
#[Digits(10)]
public int $value;
##DigitsBetween
Docs
#[DigitsBetween(2, 10)]
public int $value;
##Dimensions
Docs
#[Dimensions(ratio: 1.5)]
public UploadedFile $value;
#[Dimensions(maxWidth: 100, maxHeight: 100)]
public UploadedFile $value;
##Distinct
Docs
#[Distinct]
public string $value;
#[Distinct(Distinct::Strict)]
public string $value;
#[Distinct(Distinct::IgnoreCase)]
public string $value;
##Email
Docs
#[Email]
public string $value;
#[Email(Email::RfcValidation)]
public string $value;
#[Email([Distinct::RfcValidation, Distinct::DnsCheckValidation])]
public string $value;
#[Email(Distinct::RfcValidation, Distinct::DnsCheckValidation)]
public string $value;
##EndsWith
Docs
#[EndsWith('a')]
public string $value;
#[EndsWith(['a', 'b'])]
public string $value;
#[EndsWith('a', 'b')]
public string $value;
Docs
#[Enum(ChannelType::class)]
public string $value;
##ExcludeIf
Docs
#[ExcludeIf('other_field', 'has_value')]
public string $value;
##ExcludeUnless
Docs
#[ExcludeUnless('other_field', 'has_value')]
public string $value;
##Exists
Docs
#[Exists('users')]
public string $value;
#[Exists(User::class)]
public string $value;
#[Exists('users', 'email')]
public string $value;
#[Exists('users', 'email', connection: 'tenant')]
public string $value;
Docs
#[File]
public UploadedFile $value;
##Filled
Docs
#[Filled]
public string $value;
##GreaterThan
Docs
#[GreaterThan('other_field')]
public int $value;
##GreaterThanOrEqualTo
Docs
#[GreaterThanOrEqualTo('other_field')]
public int $value;
##Image
Docs
#[Image]
public UploadedFile $value;
Docs
#[In([1, 2, 3, 'a', 'b'])]
public mixed $value;
#[In(1, 2, 3, 'a', 'b')]
public mixed $value;
##InArray
Docs
#[InArray('other_field')]
public string $value;
##IntegerType
Docs
#[IntegerType]
public int $value;
Docs
#[IP]
public string $value;
Docs
#[IPv4]
public string $value;
Docs
#[IPv6]
public string $value;
Docs
#[Json]
public string $value;
##LessThan
Docs
#[LessThan('other_field')]
public int $value;
##LessThanOrEqualTo
Docs
#[LessThanOrEqualTo('other_field')]
public int $value;
Docs
#[Max(20)]
public int $value;
##MimeTypes
Docs
#[MimeTypes('video/quicktime')]
public UploadedFile $value;
#[MimeTypes(['video/quicktime', 'video/avi'])]
public UploadedFile $value;
#[MimeTypes('video/quicktime', 'video/avi')]
public UploadedFile $value;
##Mimes
Docs
#[Mimes('jpg')]
public UploadedFile $value;
#[Mimes(['jpg', 'png'])]
public UploadedFile $value;
#[Mimes('jpg', 'png')]
public UploadedFile $value;
Docs
#[Min(20)]
public int $value;
##MultipleOf
Docs
#[MultipleOf(3)]
public int $value;
##NotIn
Docs
#[NotIn([1, 2, 3, 'a', 'b'])]
public mixed $value;
#[NotIn(1, 2, 3, 'a', 'b')]
public mixed $value;
##NotRegex
Docs
#[NotRegex('/^.+$/i')]
public string $value;
##Nullable
Docs
#[Nullable]
public ?string $value;
##Numeric
Docs
#[Numeric]
public ?string $value;
##Password
Docs
#[Password(min: 12, letters: true, mixedCase: true, numbers: false, symbols: false, uncompromised: true, uncompromisedThreshold: 0)]
public string $value;
##Present
Docs
#[Present]
public string $value;
##Prohibited
Docs
#[Prohibited]
public ?string $value;
##ProhibitedIf
Docs
#[ProhibitedIf('other_field', 'has_value')]
public ?string $value;
#[ProhibitedIf('other_field', ['has_value', 'or_this_value'])]
public ?string $value;
##ProhibitedUnless
Docs
#[ProhibitedUnless('other_field', 'has_value')]
public ?string $value;
#[ProhibitedUnless('other_field', ['has_value', 'or_this_value'])]
public ?string $value;
##Prohibits
Docs
#[Prohibits('other_field')]
public ?string $value;
#[Prohibits(['other_field', 'another_field'])]
public ?string $value;
#[Prohibits('other_field', 'another_field')]
public ?string $value;
##Regex
Docs
#[Regex('/^.+$/i')]
public string $value;
##Required
Docs
#[Required]
public string $value;
##RequiredIf
Docs
#[RequiredIf('other_field', 'value')]
public ?string $value;
#[RequiredIf('other_field', ['value', 'another_value'])]
public ?string $value;
##RequiredUnless
Docs
#[RequiredUnless('other_field', 'value')]
public ?string $value;
#[RequiredUnless('other_field', ['value', 'another_value'])]
public ?string $value;
##RequiredWith
Docs
#[RequiredWith('other_field')]
public ?string $value;
#[RequiredWith(['other_field', 'another_field'])]
public ?string $value;
#[RequiredWith('other_field', 'another_field')]
public ?string $value;
##RequiredWithAll
Docs
#[RequiredWithAll('other_field')]
public ?string $value;
#[RequiredWithAll(['other_field', 'another_field'])]
public ?string $value;
#[RequiredWithAll('other_field', 'another_field')]
public ?string $value;
##RequiredWithout
Docs
#[RequiredWithout('other_field')]
public ?string $value;
#[RequiredWithout(['other_field', 'another_field'])]
public ?string $value;
#[RequiredWithout('other_field', 'another_field')]
public ?string $value;
##RequiredWithoutAll
Docs
#[RequiredWithoutAll('other_field')]
public ?string $value;
#[RequiredWithoutAll(['other_field', 'another_field'])]
public ?string $value;
#[RequiredWithoutAll('other_field', 'another_field')]
public ?string $value;
#[Rule('string|uuid')]
public string $value;
#[Rule(['string','uuid'])]
public string $value;
Docs
#[Same('other_field')]
public string $value;
Docs
#[Size(10)]
public string $value;
##StartsWith
Docs
#[StartsWith('a')]
public string $value;
#[StartsWith(['a', 'b'])]
public string $value;
#[StartsWith('a', 'b')]
public string $value;
##StringType
Docs
#[StringType()]
public string $value;
##TimeZone
Docs
#[TimeZone()]
public string $value;
##Unique
Docs
#[Unique('users')]
public string $value;
#[Unique(User::class)]
public string $value;
#[Unique('users', 'email')]
public string $value;
#[Unique('users', connection: 'tenant')]
public string $value;
#[Unique('users', withoutTrashed: true)]
public string $value;
#[Unique('users', ignore: 5)]
public string $value;
Docs
#[Url]
public string $value;
Docs
#[Uuid]
public string $value;