Skip to content

Changelog

All notable changes to Strictify are documented here, newest first. Install the pure-PHP library with composer require attributes-php/strictify, or the extension with pie install attributes-php/strictify-ext.

Unreleased

Added

  • Enum-typed properties now coerce a scalar to the matching case in both strict and loose mode: the backing value for backed enums, the case name for pure enums. The same coercion applies to enum class arms inside Sequence, Dict and Union specs. A value with no matching case still fails with the allowed-cases error.
enum Level
{
    case Low;
    case High;
}

class Task extends BaseModel
{
    public Level $level;
}

$task = validate(['level' => 'Low'], new Task()); // Level::Low, strict or loose
  • CallException: a new exception class. call() throws it when a required parameter has no dependency. It extends \ArgumentCountError and implements StrictifyThrowable. The message is No value provided for parameter $x of f(). It replaces the previous plain Error, so catch (StrictifyThrowable $e) now covers the whole call path. Errors thrown inside the callable or inside a dependency container still propagate unwrapped.
function hello(string $name): string { return "Hi $name"; }

try {
    call('hello', []);
} catch (CallException $e) {
    // No value provided for parameter $name of hello().
}
  • New built-in constraints ported from Respect/Validation: Email, Url, Domain, Ip (optional v4/v6), Iban, Isbn, CreditCard (Luhn), Version (semver), MacAddress, Alnum/Alpha (optional extra characters), Digit, Xdigit, Slug, Base64, Json, HexRgbColor, Even, Odd, Positive, Negative, PrimeNumber, ContainsAny. Each has its own default message and translation key.
#[Email] public string $email;
#[Ip(version: 'v4')] public string $ip;
#[ContainsAny('admin', 'owner')] public array $roles;
  • #[Url(...)] gains $protocol and $domain (string|array|null, default null). Both restrict the accepted URLs. Comparison is full and case-insensitive, with ports and userinfo ignored. https:, https:// and https count as the same protocol. Rejections use two new keys: urlProtocol (Must be a URL using an allowed protocol: {expected}) and urlDomain (Must be a URL on an allowed domain: {expected}).
#[Url(protocol: 'https', domain: ['example.com', 'example.org'])]
public string $homepage; // 'ftp://example.com' and 'https://evil.test' both fail
  • #[Email(...)] gains $domain (string|array|null, default null). It restricts the part after the @, compared in full and case-insensitively. A rejection reports the new emailDomain key (Must be an email address on an allowed domain: {expected}).
#[Email(domain: 'example.com')] public string $email; // 'a@other.test' fails
  • #[Sequence(sorted: ...)] gains $sorted. 'asc'/'ASC' or 'desc'/'DESC' sorts the validated values and renumbers the keys 0, 1, 2, .... The default null keeps the input order. It replaces the Sorted constraint, which is dropped.
#[Sequence(of: T_INT, sorted: 'asc')] public array $scores;
// [3, 1, 2] -> [0 => 1, 1 => 2, 2 => 3]
  • #[Sequence(...)] and #[Dict(...)] now enforce $uniqueValues (default false). With uniqueValues: true, a repeated element is rejected on the array property with the new uniqueValues key (Must contain only unique values). Dict::__construct() takes the flag as its third argument.
#[Sequence(of: T_STRING, uniqueValues: true)] public array $tags;
// ['a', 'b'] passes, ['a', 'a'] fails
  • New Type::null case and T_NULL constant (MAY_BE_NULL). It works as a Union arm, and is required when a nullable hint declares #[Union(...)]. It also works as a Dict key spec.
  • Dict key specs now accept every PHP-castable key type: integer, string, float, boolean, null. Per language.types.array the engine casts float and bool keys to int and null to "". Those arms therefore match the post-cast key.
#[Dict(key: T_INT | T_STRING | T_NULL, of: T_STRING)] public array $meta;
// [2.0 => 'a'] becomes key 2, [null => 'a'] becomes key ""
  • BaseModel::toDict() gains a $useAlias parameter: toDict(bool $includeSensitive = false, bool $useAlias = false): array. Output is keyed by the raw property names by default. Pass useAlias: true to key it by the resolved field names (#[Alias] and aliasGenerator, each nested model using its own generator). That output round-trips through validate().
$user->toDict();               // ['name' => 'John']
$user->toDict(useAlias: true); // ['user_name' => 'John']
  • #[ErrorMessage] accepts every key of the default error catalog as a named argument: required, type, alphaDash, between, min, max, sequenceKeys, and so on. Each defaults to that key's message, so any per-property template can be overridden, constraint errors included. The extra key is discarded: extra inputs have no owning property and keep the translator or default message.
#[ErrorMessage(required: 'Name is mandatory', min: 'At least {expected}')]
public string $name;
  • Constraint attribute arguments are validated once per field compile. An argument that makes a constraint unsatisfiable or a no-op throws an InvalidBaseModelException naming the attribute, the model and the property. Covered: inverted or NAN numeric bounds, a negative or inverted Size range, an empty In/StartsWith/EndsWith list, an empty prefix or suffix, an empty Email/Url domain or protocol restriction, and a Regex pattern that does not compile (previously a PHP warning on every call).
#[Between(10, 5)] public int $age; // InvalidBaseModelException: inverted bounds
  • Two alias mistakes now throw InvalidBaseModelException: an empty #[Alias('')], and two properties resolving to the same input key. The second covers #[Alias('id')] on two properties, or a generator collapsing $aB and $a_b onto a_b. Each used to produce a field that could never be satisfied, or two fields that silently shadowed each other.
#[Alias('id')] public int $a;
#[Alias('id')] public int $b; // InvalidBaseModelException
  • New nesting depth guard. A payload nesting models more than 512 levels deep throws a catchable MaxDepthException (a new \Error subclass) instead of exhausting the C stack. Siblings do not add depth. The counter unwinds on every exit path, including a failing deep run.
  • validate() and call() accept an int $maxDepth argument (default 512) to override the nesting limit for one run. Raise it for legitimately deep payloads, or lower it to reject deep nesting earlier. A value below 1 throws a ValueError. The exception message reports the limit that fired.
validate($payload, new Tree(), maxDepth: 32);

Changed

  • BC note: the minimum PHP version is now 8.3. PHP 8.2 reached end of life on December 31, 2026, so composer.json requires "php": "^8.3" and the build checks in config.m4 and config.w32 reject older versions. Development dependencies were upgraded to the latest releases supporting PHP 8.3: pestphp/pest 4.7.8 (PHPUnit 12.5.33), illuminate/validation and illuminate/translation 13.44.1, carthage-software/mago 1.61.0, phpbench/phpbench 1.7.1 and the symfony 7.4.x patches.
"require": {
    "php": "^8.3"
}
  • BC note: the exception hierarchy now follows PHP's split between data and programming errors. ValidationException extends \Exception. InvalidBaseModelException and MaxDepthException extend \Error. CallException extends \ArgumentCountError. BaseException is removed. Attributes\Strictify\Exceptions\StrictifyThrowable is the new marker interface all four implement, so catch (StrictifyThrowable $e) replaces catch (BaseException $e). A catch (\Exception $e) no longer swallows a malformed model.
use Attributes\Strictify\Exceptions\StrictifyThrowable;

try {
    validate($payload, new User());
} catch (StrictifyThrowable $e) {
    // ValidationException, InvalidBaseModelException, MaxDepthException or CallException
}
  • BC note: two run-time errors move from ValueError to TypeError, where PHP reports a wrong type or a wrong return value: an ErrorTranslator callable that returns something other than ?string, and a non-string #[ErrorMessage] argument. ErrorTranslator::set() now uses the engine's wording for a non-callable argument.
ErrorTranslator::set('nope');
// TypeError: Attributes\Strictify\ErrorTranslator::set(): Argument #1 ($translator)
//   must be a valid callback, function "nope" not found or invalid function name
  • The build declares ext/date as a dependency in config.m4 and config.w32. The extension no longer checks for it at startup.

  • BC note: error catalog keys are camelCase everywhere: the #[ErrorMessage] arguments and properties, and the key handed to ErrorTranslator::set() (alphaDash, sequenceKeys, urlProtocol, uniqueValues, ...). A translator registered under a snake_case key ('sequence_keys') no longer matches.

ErrorTranslator::set(fn (?string $key, string $message): ?string => $key === 'sequenceKeys' ? 'Bad order' : null);
  • BC note: a malformed model declaration now always throws InvalidBaseModelException, never a ValueError or TypeError. Covered: arguments rejected by a constructor signature (#[Union(1.5)], #[Min('fifteen')]), unresolvable class arms, unsupported Type values, unsupported Dict key types, empty Sequence/Dict arguments, an invalid Sequence(sorted:), a DateTime timezone argument, and the constraint argument rules above. Messages are unchanged; the rule just moved onto the extension's exception hierarchy, so catch (StrictifyThrowable $e) covers every malformed model. ValueError and TypeError remain for call arguments ($maxDepth below 1, the translator, call() dependencies) and for constructing an attribute object directly in userland.
#[Min('fifteen')] public int $age; // InvalidBaseModelException, not TypeError
  • BC note: the Attributes\Strictify type constants are renamed with a T_ prefix, so they no longer collide with PHP's reserved type-hint keywords: T_BOOL, T_INT, T_FLOAT, T_STRING, T_SEQUENCE, T_DICT, T_OBJECT (previously bool, int, float, string, sequence, dict, object). Update use const imports and attribute arguments.
#[Union(T_INT, T_STRING)] // was #[Union(int, string)]
  • BC note: the ErrorTranslator::set() translator is now called as fn (?string $key, string $message): ?string. The first parameter is the stable key, null for custom #[ErrorMessage] values (whose text arrives as $message). The second is the default message template. Declare the first parameter nullable.
  • BC note: #[Union(...)] on a union-typed property (e.g. int|float) must now repeat the native hint's type-hints exactly. A subset or an extra arm throws InvalidBaseModelException instead of narrowing or widening the accepted values. A nullable hint counts null as a type-hint, so a ?string property with #[Union(...)] must include T_NULL.
#[Union(T_STRING, T_NULL)] public ?string $name; // ok
#[Union(T_STRING)] public ?string $name;         // InvalidBaseModelException
  • BC note: the default error-message translator moved off ModelConfigs onto the new final class Attributes\Strictify\ErrorTranslator: ErrorTranslator::set(callable $translator): void and ErrorTranslator::get(): ?callable. ModelConfigs::setDefaultErrorMessages(), ModelConfigs::getDefaultErrorMessages() and the static $defaultErrorMessages property were removed.
  • BC note: plain toDict() on models that use #[Alias] or aliasGenerator now returns property-name keys, not alias keys. Pass useAlias: true for the previous behavior.
$user->toDict();               // ['name' => 'John']
$user->toDict(useAlias: true); // ['user_name' => 'John']
  • BC note: #[ModelConfigs] is now inherited only via the class hierarchy (parent classes). A nested model no longer inherits the enclosing model's configs: aliasGenerator, strict, extra, the string normalizations and stopAtFirstError each resolve per model class. This changes the expected input keys of nested models, and their keys in toDict(useAlias: true) output.
#[ModelConfigs(aliasGenerator: 'snake')]
class Order extends BaseModel
{
    public Customer $customer; // Customer keeps raw names unless it declares its own generator
}
  • BC note: #[Sequence] arrays now require integer keys. A string key reports Must be integer under that key's path. They are also sequential by default: keys must run 0, 1, 2, ... in order, otherwise a sequenceKeys error is reported. Pass sequential: false to allow any integer keys, gaps included. String keys stay rejected.
#[Sequence(of: T_STRING)] public array $tags;
// ['a', 'b']             ok
// [1 => 'b', 0 => 'a']  sequenceKeys error
// ['x' => 'a']          Must be integer

#[Sequence(of: T_STRING, sequential: false)] public array $sparse;
// [1 => 'b', 5 => 'a'] ok
  • ModelConfigs option values are type-checked instead of cast. A bool option accepts only true/false; a ?string option accepts only a string (at most 127 chars) or null. So strict: 1 or strToLower: 'yes' throws InvalidBaseModelException instead of flipping a flag.
#[ModelConfigs(strict: 1)] // InvalidBaseModelException
  • #[Sequence(sorted: ...)] rejects a value outside 'asc'/'desc'/null (case-insensitive) with an InvalidBaseModelException, instead of silently leaving the array unsorted.
#[Sequence(of: T_INT, sorted: 'up')] // InvalidBaseModelException
  • #[Sequence]/#[Dict]/#[Intersection] on a non-array-like property now throws InvalidBaseModelException. That covers string, int and object hints, and an intersection type without array. Previously the rule missed those hints and the attribute was ignored.
#[Sequence(of: T_INT)] public string $ids; // InvalidBaseModelException
  • Validator fixes:
  • MultipleOf(0.0) accepts 0.0, not only the integer 0.
  • DateTime rejects a date that does not exist (1994-02-30, 2023-02-29) instead of accepting the rolled-over date.
  • Iban accepts either letter case.
  • Ulid requires a first character in 0-7 (the 128-bit range).
  • Email requires a top-level domain of at least two characters, like Domain.
  • A Regex pattern containing a NUL byte is stored and matched in full.
  • Constraints are skipped for an explicit null, exactly like a missing key.
#[Regex('/^\w+$/')] public ?string $name = null;
validate(['name' => null], new User()); // passes
  • Error messages render floats at full round-trip precision (0.30000000000000004, not 0.3).
  • call() names the parameter of an internal function in its No value provided messages.

Recent history

  • #[Sensitive] properties are masked as *** in debug output: var_dump(), print_r(), debug_zval_dump(), and models inside exception traces. Property access, json_encode(), serialize() etc. keep the real values.
  • Restored string concatenation in CallFunctionHooksUser::afterValidation.
  • call() respects #[\SensitiveParameter] on target parameters. Missing dependencies are reported positionally, and stack traces redact the argument.
  • New #[Sensitive] field attribute. toDict() omits it unless includeSensitive: true. The {value} error placeholder is redacted to ***.
  • New ModelConfigs::getDefaultErrorMessages() static getter.
  • ModelConfigs extra now defaults to null and resolves per class. 'allow' stores unknown keys as dynamic properties.
  • A null ModelConfigs extra now resolves per nested model class at validation time.

See the GitHub releases and pull requests for the full history.