Search by

svinx / validator

svinx

Validator schema for PHP

Package info

github.com/svinx-org/validator

pkg:composer/svinx/validator

Statistics

Installs: 21

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

0.1.0 2026-10-20 11:23 UTC

This package is auto-updated.

Last update: 2026-10-28 13:56:22 UTC


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, Annotated constraints, Type::fromReflection().
  • TypeAdapter — validate a single value without a model, with JSON in/out.
  • Config — extra handling, alias generators, string normalization.
  • Settings — BaseSettings reads typed configuration from environment variables and .env files (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, see scripts/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.