Rules
While the library makes it easy to create your own custom validation rules, it also provides a collection of built-in rules for common use cases.
DateTime
Validates whether a value represents a valid date and/or time according to a specified format.
- Rule
- Validator
- Options:
min
(DateTimeImmutable
, optional): Sets a minimum allowed date/time.max
(DateTimeImmutable
, optional): Sets a maximum allowed date/time.format
(string): Specifies the expected date/time format. See PHP documentation.
- Examples:
Validates whether a value is a well-formatted email address.
- Rule
- Validator
- Options:
dns
(boolean, optional): If set to true, the rule performs additional DNS checks to verify that the email domain exists and has valid MX records. (Defaults to false).
- Examples:
Flag
Enforces that a value is either true
or false
(a boolean flag) and normalizes it if necessary.
- Rule
- Validator
- Options:
value
(boolean, defaultnull
): Enforces a specific value for the flag (true or false). Defaults to allowing bothtrue
andfalse
.
- Examples:
Hostname
Validates whether a value represents a valid hostname.
- Rule
- Validator
- Options:
hosts
(array of strings, optional): If provided, restricts the validation to only allow URLs with specified hostnames.dns
(boolean, default:false
): If set totrue
, performs DNS record checks.reserved
(boolean, default: false): If set totrue
, allows using reserved TLDs (e.g., ’localhost’, ’example’, ’test’, and ‘invalid’).
- Examples:
Iban
Validates whether a value conforms to the International Bank Account Number (IBAN) format.
Ip
Validates whether a value represents a valid IP address (either IPv4 or IPv6).
- Rule
- Validator
- Options:
version
(int, optional): Specifies the desired IP version to validate.- If
4
, validates only IPv4 addresses. - If
6
, validates only IPv6 addresses. - If
null
(default), validates both IPv4 and IPv6 addresses.
- If
- Examples:
Number
Validates whether a value is a number within a specified range.
- Rule
- Validator
- Options:
min
(int, float, optional): The minimum allowed value. Defaults tonull
(no minimum).max
(int, float, optional): The maximum allowed value. Defaults tonull
(no maximum).
- Examples:
Option
Validates whether a value (or values) exists within a predefined set of allowed options.
- Rule
- Validator
- Options:
options
(array of strings): Specifies the list of valid options.multiple
(boolean):- If
true
, allows the value to be an array containing multiple valid options. - If
false
, the value must be a single element from the options array.
- If
- Examples:
Password
Enforces password complexity requirements, ensuring that user passwords meet a certain level of security.
- Rule
- Validator
- Options:
min
(int, default: 8): Specifies the minimum required password length.upper
(bool, default:true
): Requires at least one uppercase letter.lower
(bool, default:true
): Requires at least one lowercase letter.number
(bool, default:true
): Requires at least one numeric character.special
(bool, default:true
): Requires at least one special character.
- Examples:
Slug
Specifically validates strings intended to be used as URL-friendly slugs (e.g., in blog post titles or product identifiers).
- Rule
- Validator
- Default Behavior: By default, enforces these constraints:
- Minimum length: 2 characters
- Maximum length: 64 characters
- Allowed characters: Lowercase letters, numbers, hyphens (
-
), and underscores (_
).
- Examples:
Text
Validates textual input, providing flexible constraints based on length and regular expression patterns.
- Rule
- Validator
- Options:
minLength
(int, optional): Specifies the minimum allowed length of the text. Defaults tonull
(no minimum).maxLength
(int, optional): Specifies the maximum allowed length of the text. Defaults tonull
(no maximum).regExp
(string, optional): Provides a regular expression pattern that the text must match. Defaults tonull
(no pattern-based validation).
- Examples:
Url
Validates whether a value represents a well-formed URL (Uniform Resource Locator).
- Rule
- Validator
- Options:
schemes
(array of strings, default:['http', 'https']
): Specifies a list of allowed URL schemes (e.g., ‘http’, ‘https’, ‘ftp’, etc.).hosts
(array of strings, optional): If provided, restricts the validation to only allow URLs with specified hostnames.dns
(boolean, default:false
): If set totrue
, performs DNS record checks.reserved
(boolean, default: false): If set totrue
, allows using reserved TLDs (e.g., ’localhost’, ’example’, ’test’, and ‘invalid’).
- Examples:
Uuid
Validates whether a value conforms to the Universally Unique Identifier (UUID) format, optionally specifying a particular UUID version.
- Rule
- Validator
- Options:
version
(int, optional): Specifies the required UUID version. Valid options include:1
: Validates Version 1 UUIDs3
: Validates Version 3 UUIDs4
: Validates Version 4 UUIDs5
: Validates Version 5 UUIDs7
: Validates Version 7 UUIDs- If
null
(default), validates any valid UUID version.
- Examples: