Luminova Framework

PHP Luminova: Class-Based Input Validation Rules

Last updated: 2026-11-28 20:57:12

Fluent validation rules in Luminova for field types, ranges, formats, and custom checks, with arguments and error messages.

The Input Validation Rules allows you to define a structured validation rules when validating user's input using the Input Validator. Each definition contains a rule name, optional arguments, and an error message. The validator applies these definitions to the fields they are assigned to.

Typed arguments and editor autocomplete make the definition useful when rules take several options. The validator also accepts String-based Validation Rules.


Rule Structure

Each Rule method returns a structured array:

IndexTypeDescription
0stringRule name
1array|nullRule arguments (if any)
2stringValidation error message

Example:

Rule::minLength(5, 'Minimum 5 characters required.');

Resolves internally to:

['minlength', [5], 'Minimum 5 characters required.'];

Usages

Defining Rules

Rules are assigned per field using an array:

$validation->rules = [
    'username' => [
        Rule::required('Username is required.'),
        Rule::minLength(3, 'Must be at least 3 characters.'),
    ],
];

Each field can contain multiple rules, executed in order.


Applying Rules

You can define validation rules directly within your controller or service class:

$this->input->setRules([
    'email' => [
        Rule::required(),
        Rule::email(),
    ],

    'username' => [
        Rule::required(),
        Rule::alphanumeric(),
        Rule::minLength(3),
        Rule::maxLength(20),
    ],

    'age' => [
        Rule::required(),
        Rule::between(18, 50),
    ],
]);

Passing Arguments

Some rules accept parameters to refine validation behavior.

Examples:

Ensures the value contains at least 6 characters.

Rule::minLength(6);

With comparison:

Ensures the field value is not equal to another field.

Rule::notEqual('password');

Custom Messages

Each rule can define its own error message:

Rule::required('Email is required.');

If no message is provided, the validator will fallback to a default message (depending on rule-type).


Class Definition

  • Full namespace: Luminova\Security\Rule
  • This class is final.

Methods

for

Build a validation rule definition.

public static function for(string $rule, ?array $argument = null, ?string $error = null): array

This method creates a structured rule array used by the validation engine.It supports passing optional arguments and a custom error message.

If no error message is provided, a default message is generated using:The {field} is invalid ({rule}).

Parameters:

ParameterTypeDescription
$rulestringRule name (e.g. required, minlength, not_equal).
$argumentarray<array-key, mixed>&#124;nullOptional rule arguments (e.g. [5], ['a','b']).
$errorstring|nullCustom validation error message.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.

Throws:


isRule

Check if a validation rule exists.

public static function isRule(string $name): bool

This method does not perform validation. It only verifies whetherthe given rule name is supported by the validator.

Parameters:

ParameterTypeDescription
$namestringThe rule name to validate.

Return:

bool - Return true if rule exists, false otherwise.


required

Require that the field is not empty or null.

public static function required(?string $error = null): array

Rule name: required

Fails when the value is an empty string, null, or otherwiseconsidered empty by the validator's internal isEmpty check.

Parameters:

ParameterTypeDescription
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


nullable

Indicate that the field can be empty or null.

public static function nullable(): array

Rule name: nullable

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


isString

Require that the value is a PHP string.

public static function isString(?string $error = null): array

Rule name: string

Fails when is_string($value) returns false.

Parameters:

ParameterTypeDescription
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


numeric

Require that the value is numeric.

public static function numeric(?string $error = null): array

Rule name: numeric

Delegates to PHP's is_numeric(), so integer strings andfloat strings both pass.

Parameters:

ParameterTypeDescription
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


boolean

Require that the value is a valid boolean representation.

public static function boolean(?array $booleans = null, ?string $error = null): array

Rule name: boolean

Accepts true, false, 1, 0, "true", "false", "yes","no", etc., as determined by the validator.

Parameters:

ParameterTypeDescription
$booleansarray<int,mixed>&#124;nullOptional list of accepted boolean representations.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


integer

Require that the value is an integer.

public static function integer(?string $type = 'unsigned', ?string $error = null): array

Rule name: integer

An optional type qualifier constrains the integer further:

  • positive — value must be greater than zero.
  • negative — value must be less than zero.
  • unsigned — value must be greater or equals to zero.
  • null — any valid integer.

Values containing a decimal point (.) are always rejected.

Parameters:

ParameterTypeDescription
$typestring|nullOptional sub-type: 'positive', 'negative', unsigned, or null for any.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


digit

Require that the value consists of decimal digits only.

public static function digit(?string $error = null): array

Rule name: digit

Delegates to ctype_digit(). Floats, negative numbers, andleading/trailing spaces will fail.

Parameters:

ParameterTypeDescription
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


isFloat

Require that the value is a floating-point number.

public static function isFloat(?string $type = 'unsigned', ?string $error = null): array

Rule name: float

An optional type qualifier constrains the float:

  • positive — value must be greater than zero.
  • negative — value must be less than zero.
  • unsigned — value must be greater or equals to zero.
  • any — any float (default).

Values containing scientific notation (e.g. 1e5) are rejected.Integer values without a decimal point are also rejected.

Parameters:

ParameterTypeDescription
$typestring|nullOptional sub-type: positive, negative, unsigned, or null for any.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


decimal

Require that the value is a decimal number (must contain a decimal point).

public static function decimal(?string $type = 'unsigned', ?string $error = null): array

Rule name: decimal

Behaves like self::float() but additionally requires the valueto contain a . character, ruling out plain integers.

An optional type qualifier constrains the result:

  • positive — value must be greater than zero.
  • negative — value must be less than zero.
  • unsigned — value must be greater or equals to zero.
  • null — any decimal (default).

Parameters:

ParameterTypeDescription
$typestring|nullOptional sub-type: positive, negative, unsigned, or null for any.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


alphanumeric

Require that the value is alphanumeric.

public static function alphanumeric(?string $error = null): array

Rule name: alphanumeric

Delegates to ctype_alnum(). The value must be a non-empty stringcontaining only ASCII letters (A-Z, a-z) and digits (0-9).

Parameters:

ParameterTypeDescription
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


alphabet

Require that the value contains alphabetic characters only.

public static function alphabet(?string $error = null): array

Rule name: alphabet

Delegates to ctype_alpha(). Digits, spaces, and special characterswill cause validation to fail.

Parameters:

ParameterTypeDescription
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


email

Require that the value is a valid e-mail address.

public static function email(
    array $rejectDomains = [],
    bool $allowIdn = false,
    ?string $error = null
): array

Rule name: email

Optionally reject specific domains and/or allow internationalizeddomain names (IDN).

Parameters:

ParameterTypeDescription
$rejectDomainsarray<array-key, mixed>List of domain strings to reject (e.g. ['example.com']).
$allowIdnboolWhether to accept IDN e-mail addresses (default false).
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


url

Require that the value is a valid URL.

public static function url(bool $allowIdn = false, bool $httpOnly = true, ?string $error = null): array

Rule name: url

Parameters:

ParameterTypeDescription
$allowIdnboolWhether to allow internationalized domain names.
$httpOnlyboolRestrict to http/https schemes only.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


json

Require that the value is a valid JSON string.

public static function json(?string $error = null): array

Rule name: json

Delegates to PHP's json_validate(). The value must be a string.

Parameters:

ParameterTypeDescription
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


isArray

Require that the value is an array or a JSON-encoded array string.

public static function isArray(?string $error = null): array

Rule name: array

Passes when is_array($value) is true, or when the value is astring that decodes to an array via json_decode().

Parameters:

ParameterTypeDescription
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


binary

Require that the value is a valid binary representation.

public static function binary(
    bool $strict = true,
    bool $allowPrintable = false,
    ?string $error = null
): array

Rule name: binary

Behavior is controlled by the two flags, matching the validator's

  • $strict = true (default) — only characters 0 and 1 are accepted.
  • $strict = false — PHP-style binary literals (e.g. 0b1010) are also accepted.
  • $allowPrintable = true — when non-strict, printable ASCII strings are accepted as a fallback.
  • $allowPrintable = false — printable ASCII fallback is disabled (default).

Parameters:

ParameterTypeDescription
$strictboolWhether to enforce strict binary-digit-only validation (default true).
$allowPrintableboolWhether to accept printable ASCII as a fallback when non-strict (default false).
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


hexadecimal

Require that the value is a valid hexadecimal string.

public static function hexadecimal(?string $error = null): array

Rule name: hexadecimal

Delegates to ctype_xdigit(). The value must not be an array.

Parameters:

ParameterTypeDescription
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


min

Require that the value is greater than or equal to a minimum.

public static function min(float|int $value, ?string $error = null): array

Rule name: min

Universal rule — type is auto-detected:

  • Numeric: the numeric value is compared.
  • String: the character length is compared.
  • Array: the element count is compared.

Parameters:

ParameterTypeDescription
$valuefloat|intThe minimum allowed value, length, or count.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


max

Require that the value is less than or equal to a maximum.

public static function max(float|int $value, ?string $error = null): array

Rule name: max

Universal rule — type is auto-detected:

  • Numeric: the numeric value is compared.
  • String: the character length is compared.
  • Array: the element count is compared.

Parameters:

ParameterTypeDescription
$valuefloat|intThe maximum allowed value, length, or count.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


fixed

Require that the value is exactly equal to the given number.

public static function fixed(float|int $value, ?string $error = null): array

Rule name: fixed

Universal rule — type is auto-detected:

  • Numeric: the numeric value is compared.
  • String: the character length is compared.
  • Array: the element count is compared.

Parameters:

ParameterTypeDescription
$valuefloat|intThe exact value, length, or count the field must equal.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


between

Require that the value falls within a range (inclusive).

public static function between(float|int $min = 1, float|int $max = 100, ?string $error = null): array

Rule name: between

Works with both numbers and strings:

  • Numeric values: the number is compared directly.
  • Strings: the character length is compared.

Parameters:

ParameterTypeDescription
$minfloat|intThe minimum value or character length (default 1).
$maxfloat|intThe maximum value or character length (default 100).
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


length

Require that the string length is exactly $length characters.

public static function length(int $length, ?string $error = null): array

Rule name: length

Type-specific: only applied to string values.Maps to the 'fixed' comparator internally.

Parameters:

ParameterTypeDescription
$lengthintThe required exact character length.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


minLength

Require that the string length is at least $length characters.

public static function minLength(int $length, ?string $error = null): array

Rule name: minlength

Type-specific: only applied to string values.Maps to the 'min' comparator internally.

Parameters:

ParameterTypeDescription
$lengthintThe minimum character length.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


maxLength

Require that the string length does not exceed $length characters.

public static function maxLength(int $length, ?string $error = null): array

Rule name: maxlength

Type-specific: only applied to string values.Maps to the 'max' comparator internally.

Parameters:

ParameterTypeDescription
$lengthintThe maximum character length.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


limit

Require that the numeric value equals exactly $value.

public static function limit(float|int $value, ?string $error = null): array

Rule name: limit

Type-specific: only applied to numeric values.

Similar to fixed() but operates in the limit contextused by the validator for numeric fields.

Parameters:

ParameterTypeDescription
$valuefloat|intThe required exact numeric value.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


minLimit

Require that the numeric value is at least $value.

public static function minLimit(float|int $value, ?string $error = null): array

Rule name: minlimit

Type-specific: only applied to numeric values.Maps to the min() comparator internally.

Parameters:

ParameterTypeDescription
$valuefloat|intThe minimum numeric value.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


maxLimit

Require that the numeric value does not exceed $value.

public static function maxLimit(float|int $value, ?string $error = null): array

Rule name: maxlimit

Type-specific: only applied to numeric values.Maps to the max() comparator internally.

Parameters:

ParameterTypeDescription
$valuefloat|intThe maximum numeric value.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


size

Require that the array contains exactly $count elements.

public static function size(int $count, ?string $error = null): array

Rule name: size

Type-specific: only applied to array values.Maps to the fixed() comparator internally.

Parameters:

ParameterTypeDescription
$countintThe required exact element count.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


minSize

Require that the array contains at least $count elements.

public static function minSize(int $count, ?string $error = null): array

Rule name: minsize

Type-specific: only applied to array values.Maps to the 'min' comparator internally.

Parameters:

ParameterTypeDescription
$countintThe minimum element count.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


maxSize

Require that the array does not exceed $count elements.

public static function maxSize(int $count, ?string $error = null): array

Rule name: maxsize

Type-specific: only applied to array values.Maps to the 'max' comparator internally.

Parameters:

ParameterTypeDescription
$countintThe maximum element count.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


latitude

Require that the value is a valid geographic latitude coordinate.

public static function latitude(bool $strict = false, int $precision = 6, ?string $error = null): array

Rule name: latitude

Accepts values in the range −90 to +90.

Parameters:

ParameterTypeDescription
$strictboolWhether to enable strict format checking (default false).
$precisionintMaximum number of decimal digits to allow (default 6).
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


lat

Short alias for latitude().

public static function lat(bool $strict = false, int $precision = 6, ?string $error = null): array

Rule name: lat

Useful for shorter rule expressions while retaining the same behavior.

Parameters:

ParameterTypeDescription
$strictboolWhether to enable strict format checking (default false).
$precisionintMaximum number of decimal digits to allow (default 6).
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


longitude

Require that the value is a valid geographic longitude coordinate.

public static function longitude(bool $strict = false, int $precision = 6, ?string $error = null): array

Rule name: longitude

Accepts values in the range −180 to +180. Delegates to Math::isLng().

Parameters:

ParameterTypeDescription
$strictboolWhether to enable strict format checking (default false).
$precisionintMaximum number of decimal digits to allow (default 6).
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


lng

Alias for longitude().

public static function lng(bool $strict = false, int $precision = 6, ?string $error = null): array

Rule name: lng

Useful for shorter rule expressions while retaining the same behavior.

Parameters:

ParameterTypeDescription
$strictboolWhether to enable strict format checking.
$precisionintMaximum number of decimal digits to allow.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


latLng

Require that the value is a valid latitude,longitude coordinate pair.

public static function latLng(bool $strict = false, int $precision = 6, ?string $error = null): array

Rule name: latlng

The field value must be a comma-separated string (e.g. "51.5074,-0.1278").The lat and lng parts are split from the value, $strict and $precisionare applied to validation.

Parameters:

ParameterTypeDescription
$strictboolWhether to enable strict format checking (default false).
$precisionintMaximum number of decimal digits to allow (default 6).
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


ip

Require that the value is a valid IP address.

public static function ip(int $version = 0, ?string $error = null): array

Rule name: ip

Pass 4 for IPv4-only, 6 for IPv6-only,or 0 to accept any version. When no argument is given the validatordefaults to 0 (any version).

Parameters:

ParameterTypeDescription
$versionintIP version to validate: 4, 6, or 0 for any (default 0).
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


uuid

Require that the value is a valid UUID string.

public static function uuid(int $version = 4, ?string $error = null): array

Rule name: uuid

Validates the string format against the specified UUID version.When no argument is given the validator defaults to version 4.

Parameters:

ParameterTypeDescription
$versionintUUID version to validate (default 4).
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


phone

Require that the value is a valid phone number.

public static function phone(int $min = 10, int $max = 15, ?string $error = null): array

Rule name: phone

The digit count of the number (after stripping formatting characters) must fall between $minand $max inclusive.

Parameters:

ParameterTypeDescription
$minintMinimum digit count (default 10).
$maxintMaximum digit count (default 15).
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


path

Require that the value is a valid file-system path.

public static function path(?string $access = null, ?string $error = null): array

Rule name: path

The optional $access argument controls what "valid" means:

  • null — the value must match a well-formed absolute path pattern (default).
  • readable — the path must exist and be readable (is_readable()).
  • writable — the path must exist and be writable (is_writable()).

Parameters:

ParameterTypeDescription
$accessstring|nullAccess mode: readable, or writable.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


scheme

Require that the value is a string with a valid URI scheme.

public static function scheme(?string $protocol = null, ?string $error = null): array

Rule name: scheme

When $protocol is empty (default) any syntactically valid scheme isaccepted (e.g. http://, ftp://, mailto:).When $protocol is provided (e.g. 'https') the value must beginwith exactly that scheme followed by :.

Parameters:

ParameterTypeDescription
$protocolstringExpected scheme without :// (e.g. 'https'), or null for any.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


equals

Require that the value equals the value of another field in the same request body.

public static function equals(string $field, ?string $error = null): array

Rule name: equals

Useful for "confirm password" style checks where two fields must match.Comparison is strict (===) after both values are normalized through toValue().

Parameters:

ParameterTypeDescription
$fieldstringThe name of the other field to compare against.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


notEqual

Require that the value does not equal the value of another field in the same request body.

public static function notEqual(string $field, ?string $error = null): array

Rule name: not_equal

Comparison is strict (!==) after both values are normalized.

Parameters:

ParameterTypeDescription
$fieldstringThe name of the other field to compare against.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


isValue

Require that the value is exactly a specified literal value.

public static function isValue(mixed $value, ?string $error = null): array

Rule name: is_value

Unlike equals(), which compares against another field'sruntime value, this compares the field against a hard-coded literalthat is embedded directly in the rule expression.

Parameters:

ParameterTypeDescription
$valuemixedThe exact literal value the field must equal.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


regex

Build the regex rule. The validator currently treats the supplied expression as literal text.

public static function regex(string $pattern, ?string $error = null): array

Rule name: regex

Supply the literal text to search for.

Parameters:

ParameterTypeDescription
$patternstringText to look for in the field.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


inArray

Require that the value is present in a given list.

public static function inArray(array $values, bool $strict = false, ?string $error = null): array

Rule name: in_array

When $strict is false (default) the comparison is type-coercing (==).When $strict is true strict type-safe comparison (===) is used.

Parameters:

ParameterTypeDescription
$valuesarray<array-key, mixed>The list of accepted values.
$strictboolWhether to use strict (===) comparison (default false).
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


isList

Require that the value is a comma-separated list with a minimum number of items.

public static function isList(int $min = 1, ?string $error = null): array

Rule name: is_list

The value must be a string. Items are split by ,, trimmed, and emptysegments are discarded before the count is compared against $min.

Parameters:

ParameterTypeDescription
$minintMinimum number of non-empty list segments required (default 1).
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


keyExists

Require that at least one of the given keys exists in the array value.

public static function keyExists(array $keys, ?string $error = null): array

Rule name: key_exists

Passes when the intersection of $keys and the field value's array keysis non-empty. Fails if the field value is not an array, or if none ofthe specified keys are present.

Parameters:

ParameterTypeDescription
$keysarray<array-key, mixed>List of keys of which at least one must be present.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


keysExists

Require that all specified keys exist in the array value.

public static function keysExists(array $keys, bool $strict = false, ?string $error = null): array

Rule name: keys_exists

When $strict is false (default) only a subset check is performed,extra keys in the value are allowed.When $strict is true the value must contain exactly $keys andno additional keys (bidirectional diff check).

Parameters:

ParameterTypeDescription
$keysarray<array-key, mixed>The list of keys that must all be present.
$strictboolWhen true, no keys outside of $keys may exist (default false).
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


username

Require that the value is a valid username.

public static function username(
    bool $allowUppercase = true,
    bool $allowUnicode = false,
    array $reservedUsernames = [],
    ?string $error = null
): array

Rule name: username

Validates:

  • Length: 3–64 UTF-8 characters.
  • Allowed characters: letters, numbers, _, -, . ([\p{L}\p{N}_.-]).
  • No whitespace.
  • No all-uppercase (even when $allowUppercase is true).
  • No all-numbers string.
  • No leading/trailing hyphens or dots.
  • No consecutive hyphens (--), underscores (__), or dots (..).

Parameters:

ParameterTypeDescription
$allowUppercaseboolWhether uppercase letters are permitted (default true).
When true, all-uppercase strings are still rejected.
When false, any uppercase letter causes failure.
$allowUnicodeboolAllow Unicode letters and numbers. Defaults to false.
$reservedUsernamesarray<array-key, mixed>List of reserved names that must not be used (case-insensitive).
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


callback

Validate the field value with a custom callable.

public static function callback(callable|array|string $fn, ?string $error = null): array

Rule name: callback

The callable receives ($fieldValue, $fieldName) and must returna truthy value to indicate success or a falsy value for failure.

Supported callable formats:

  • Closure: fn($value, $field): bool
  • Plain function name: 'myFunction'
  • Instance method: 'MyClass@method'
  • Static method string: 'MyClass::method'
  • Static array: [MyClass::class, 'method']

Parameters:

ParameterTypeDescription
$fncallable&#124;array<array-key, mixed>&#124;stringThe custom validation callback.
$errorstring|nullOptional error message if validation failed.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


default

Assign a default value to the field when it is empty or missing.

public static function default(mixed $value): array

Rule name: default

When the field value is empty the validator replaces it with $valuein the validated body and on the request object (when available).No validation error is recorded for this rule.

Parameters:

ParameterTypeDescription
$valuemixedThe default value to assign to the field.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


fallback

Alias for default().

public static function fallback(mixed $value): array

Rule name: fallback

Identical behavior, handled by the same case 'fallback': branchin the validator. Use whichever name reads more naturally in context.

Parameters:

ParameterTypeDescription
$valuemixedThe fallback value to assign when the field is empty.

Return:

array{0: string, 1: ?array, 2: string} - Return an array that resolve to rule.


nullbyte

Reject null bytes by default. Set allow to require one instead.

public static function nullbyte(bool $allow = false, ?string $error = null): array

Parameters:

ParameterTypeDescription
$allowboolWhether a null byte must be present.
$error?stringOptional custom message.

Return:

array{0:string,1:array,2:string} - The rule name, arguments, and error message.


luhn

Check that a number passes the Luhn checksum.

public static function luhn(?string $error = null): array

Parameters:

ParameterTypeDescription
$error?stringOptional custom message.

Return:

array{0:string,1:array,2:string} - The rule name, arguments, and error message.


name

Check a name against the allowed character and length rules.

public static function name(
    bool $forceFirstName = false,
    int $min = 2,
    int $max = 150,
    ?string $error = null
): array

Parameters:

ParameterTypeDescription
$forceFirstNameboolRequire at least two name components.
$minintMinimum length. Defaults to 2.
$maxintMaximum length. Defaults to 150.
$error?stringOptional custom message.

Return:

array{0:string,1:array,2:string} - The rule name, arguments, and error message.


contains

Create a substring rule. The current validation check requires the substring when allow is true and rejects it when false.

public static function contains(
    string $needle,
    bool $allow = false,
    ?string $error = null
): array

Parameters:

ParameterTypeDescription
$needlestringSubstring to check.
$allowboolRequire the substring when true.
$error?stringOptional custom message.

Return:

array{0:string,1:array,2:string} - The rule name, arguments, and error message.


formatField

Format an input field name for use in an error message.

public static function formatField(string $field): string

Parameters:

ParameterTypeDescription
$fieldstringField name to format.

Return:

string - The formatted field name.