Search by

mj-zana / validation

mjnable

Attribute-based validation of objects for Zana applications

Package info

github.com/mjnable/zana-validation

pkg:composer/mj-zana/validation

Statistics

Installs: 16

Dependents: 1

Suggesters: 1

Stars: 0

Open Issues: 0

v2.1.0 2026-10-25 20:38 UTC

This package is auto-updated.

Last update: 2026-10-25 20:45:24 UTC


README

Attribute-based validation of objects, typically input DTOs. No dependencies. Requires PHP 8.5 or later.

composer require mj-zana/validation

Usage

use Zana\Validation\Constraint\{Email, Length, NotBlank, Range, Valid};
use Zana\Validation\Validator;

final class RegisterPatient
{
    public function __construct(
        #[NotBlank] #[Length(min: 2, max: 50)] public string $name,
        #[Email] public ?string $email = null,
        #[Range(min: 50, max: 250)] public ?float $heightCm = null,
        #[Valid] public ?Address $address = null,
    ) {}
}

$violations = (new Validator())->validate($dto);
$violations->toArray();   // ['name' => ['This value should not be blank.'], 'address.city' => [...]]

(new Validator())->assertValid($dto);   // throws ValidationException

Every constraint on every property is checked, and all violations are reported. Constraints other than NotBlank and NotNull pass on null.

Constraints

NotBlank, NotNull, Length(min, max), Range(min, max), Positive, Email, Url (http/https), Regex(pattern), Choice(choices), Count(min, max) and Valid (validates nested objects, or each object of an array).

Each accepts a message with {min}, {max} or {choices} placeholders. Custom constraints implement ConstraintInterface and are attributes.

New in 2.1

Each Violation keeps its message template and parameters. Give the Validator a MessageTranslatorInterface to translate them; mj-zana/translation registers one, so custom messages can be keys: #[NotBlank(message: 'contact.error.name_required')].

License

MIT — see LICENSE.