attributes-php/validation

The PHP Pydantic validation library which supports data validation via type-hinting

v0.1.0-beta 2025-04-28 08:01 UTC

This package is auto-updated.

Last update: 2025-05-12 18:18:01 UTC


README

Latest Version Software License

Attributes Validation is the Pydantic validation library for PHP which allows you to validate data via type hints

Features

  • Validates data via type-hinting
  • Converts raw dictionaries into classes
  • Support for custom validation rules

Requirements

We aim to support versions that haven't reached their end-of-life.

How it works?

<?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", (...) }

Installation

composer require attributes-php/validation

Attributes Validation was created by André Gil and is open-sourced software licensed under the MIT license.