Attributes Validation is the Pydantic validation library for PHP which allows you to validate data via type hints
- Validates data via type-hinting
- Converts raw dictionaries into classes
- Support for custom validation rules
- PHP 8.1+
- respect/validation
We aim to support versions that haven't reached their end-of-life.
<?php
use Attributes\Validation\Validator;
use Respect\Validation\Rules as Rules;
class Person
{
#[Rules\Min(0)]
public float|int $age;
public ?DateTime $birthday;
}
$rawData = [
'age' => '30',
'birthday' => '1994-01-01T09:00:00+00:00',
];
$validator = new Validator();
$person = $validator->validate($rawData, new Person);
var_dump($person->age); // int(100)
var_dump($person->birthday); // object(DateTime) { ["date"] => string(26) "1994-01-01 09:00:00.000000", (...) }
composer require attributes-php/validation
Attributes Validation was created by André Gil and is open-sourced software licensed under the MIT license.