svinx / validator
Validator schema for PHP
Requires
None
Requires (Dev)
- phpunit/phpunit: ^13.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Model and schema validation for PHP 8.4, in the spirit of Pydantic.
Declare typed properties, and the library validates, converts and serializes raw input (decoded JSON, environment variables, form data) into safe objects.
use Svinx\Validator\BaseModel; use Svinx\Validator\Field; use Svinx\Validator\Validators\EmailStr; class User extends BaseModel { #[Field(alias: 'user_id', gt: 0)] public int $id; #[Field(alias: 'username', min_length: 2)] public string $name = 'Anonyme'; #[Field(alias: 'email')] public EmailStr $email; } $user = new User([ 'user_id' => 42, 'username' => 'Jean', 'email' => 'jean@example.com', ]); $user->model_dump(); // ['user_id' => 42, 'username' => 'Jean', 'email' => 'jean@example.com'] new User(['user_id' => 0]); // Svinx\Validator\ValidationError : Le champ 'id' doit être > 0
An invalid value never yields a half-filled model: a ValidationError is raised
before the object is constructed.
Features
- Declarative validation —
#[Field]constraints: aliases, ranges, lengths, patterns, closed choices, defaults. - Nested models — models inside models, recursive serialization.
- Validated types —
EmailStr,HttpUrl,IPv4Address,IPv6Address,UUID,Decimal,Date,Datetime,SecretStr. - Custom validators —
#[FieldValidator], the PHP equivalent of@field_validator(before/after modes, multiple fields, wildcards,always). - Typing —
Type::list(...),Type::map(...), unions, literals, enums,TypeVar,Annotatedconstraints,Type::fromReflection(). - TypeAdapter — validate a single value without a model, with JSON in/out.
- Config —
extrahandling, alias generators, string normalization. - Settings —
BaseSettingsreads typed configuration from environment variables and.envfiles (prefixes, nested settings, secrets). - Schemas —
model_schema()/json_schema()produce JSON Schema.
Installation
composer require svinx/validator
Requires PHP 8.4+ (mbstring and json extensions).
Documentation
The full course is available in several languages, translated from the French
source (docs/fr):
- 🇫🇷 Français
- 🇬🇧 English
- 🇪🇸 Español, 🇧🇷 Português, 🇩🇪 Deutsch, 🇰🇷 한국어 — generated by
scripts/translate.py, seescripts/README.md
Build the site locally with MkDocs:
pip install mkdocs-material mkdocs-static-i18n mkdocs serve
Examples and tests
Every example used in the documentation lives in docs_src/ and is
imported by the test suite through the Svinx\Validator\Examples namespace, so
documentation and tests cannot drift apart.
composer install composer test # phpunit
Project layout
src/ the library
docs_src/ runnable examples (namespace Svinx\Validator\Examples)
tests/ phpunit suite
docs/fr, docs/en documentation sources (fr is the source language)
scripts/ translation pipeline
Contributing
See CONTRIBUTING.md. Translations are automated (see
scripts/README.md); the French documentation is the source
of truth.
License
MIT — see LICENSE.