Skip to content
Antares

Validation Attributes

All attributes live in Antares\Validation\Attributes and go on constructor parameters. Each one returns an error message when it fails; all failures for a field are collected.

These only check string values.

AttributePasses whenError message
#[NotBlank]The string isn’t empty or only whitespaceThe value must not be blank.
#[MinLength(n)]At least n characters (multibyte-aware)The value must be at least n characters long.
#[MaxLength(n)]At most n charactersThe value must be at most n characters long.
#[Size(min, max)]Between min and max charactersThe value must be between min and max characters.
#[Email]PHP’s FILTER_VALIDATE_EMAIL accepts itThe value must be a valid email address.
#[Url]PHP’s FILTER_VALIDATE_URL accepts itThe value must be a valid URL.
#[Ip]A valid IPv4 or IPv6 addressThe value must be a valid IP address.
#[Uuid]UUID format 8-4-4-4-12 hex, any version, case-insensitiveThe value must be a valid UUID.
#[Pattern('/regex/')]preg_match finds a matchdoes not match pattern
#[Alpha]Letters only (ctype_alpha)The value must contain only letters.
#[AlphaNumeric]Letters and digits only (ctype_alnum)The value must contain only letters and numbers.
#[Numeric]Digits only (ctype_digit): no sign, no decimal pointThe value must contain only digits.
#[HexColor]#fff or #ffffffThe value must be a valid hex color.
#[Phone]7–20 characters of digits, spaces, -, (, ), optional leading +The value must be a valid phone number.
#[Json]json_decode succeedsThe value must be a valid JSON string.
#[Date(format)]Matches the date format exactly (default Y-m-d)The value must be a valid date in format Y-m-d.
#[DateTime(format)]Matches the format exactly (default Y-m-d H:i:s)The value must be a valid datetime in format Y-m-d H:i:s.
#[DateTimeImmutable(format)]Same check as #[DateTime]The value must be a valid datetime in format Y-m-d H:i:s.

#[Date], #[DateTime] and #[DateTimeImmutable] check the string; the field itself stays a string. Convert it yourself, e.g. new \DateTimeImmutable($request->startsAt). Note that #[DateTimeImmutable] shares its short name with PHP’s \DateTimeImmutable class; alias the import if you need both in one file.

These only check numeric values (numbers or numeric strings).

AttributePasses whenError message
#[Min(n)]Value ≥ nThe value must be at least n.
#[Max(n)]Value ≤ nThe value must be at most n.
#[Between(min, max)]min ≤ value ≤ maxThe value must be between min and max.
#[Positive]Value > 0The value must be a positive number.
#[Negative]Value < 0The value must be a negative number.
AttributePasses whenError message
#[NotNull]The value isn’t nullThe value must not be null.
#[In([...])]The value is in the list, compared strictly ('1' doesn’t match 1)The value must be one of: a, b.
#[InEnum(MyEnum::class)]MyEnum::tryFrom($value) finds a case. The enum must be a backed enumThe value must be one of the following: a, b.
#[ArrayOf(type)]The value is an array whose items are all of type: 'string', 'int', 'float', 'bool', 'array', or a class name (instanceof)The value must be an array. / Item at index i must be of type T.

#[File] validates an uploaded file on a controller parameter typed UploadedFileInterface. It is not used inside DTOs.

use Antares\Validation\Attributes\File;
use Psr\Http\Message\UploadedFileInterface;
#[Post('/avatars', 201)]
public function upload(
#[File(maxSize: 2048, mimes: ['image/png', 'image/jpeg'], extensions: ['png', 'jpg'])]
UploadedFileInterface $avatar,
): array {}
OptionDefaultChecks
maxSize0 (no limit)Size in kilobytes
mimes[] (any)MIME type detected from the file’s contents
extensions[] (any)Extension of the client’s file name, lowercased
requiredtrueRejects an empty file field (no file chosen)

A failed check returns 422 with the error under the parameter name. If the request has no field with that name at all, it is rejected with 400 Missing file: avatar before #[File] runs, even with required: false.

AttributeDescription
#[Dto]Marks a class as hydratable from request data
#[Strict]Rejects request fields the DTO doesn’t declare (400)

When validation fails, the framework returns 422 with every failed rule, per field:

{
"type": "https://antares.dev/errors",
"title": "Validation failed",
"status": 422,
"errors": {
"email": ["The value must be a valid email address."],
"name": ["The value must be at least 2 characters long."]
}
}