Getting started

Install the package

From an existing Composer project, run:

composer require einlinuus/php-validator

If you are starting in an empty directory, initialize Composer first:

composer init
composer require einlinuus/php-validator

Load Composer's autoloader in your application:

require_once __DIR__ . '/vendor/autoload.php';

Validate a value

Create a Validator with the raw value, then chain the rules that the value must satisfy:

<?php

use EinLinuus\PhpValidator\EinLinuus\PhpValidator\Validator;
use EinLinuus\PhpValidator\EinLinuus\PhpValidator\ValidatorException;

require_once __DIR__ . '/vendor/autoload.php';

$validator = new Validator('hello world');

try {
    $validator
        ->isString('Input must be a string')
        ->isLowercase('Input must be lowercase')
        ->min(3, 'Input must be at least 3 characters long')
        ->max(12, 'Input must be at most 12 characters long');

    $validated = $validator->get();
} catch (ValidatorException $exception) {
    echo 'Invalid: ' . $exception->getMessage();
}

Each rule returns the same validator, so calls can be chained. Validation stops at the first failing rule.

Read the validated value

Call get() only after the validation chain succeeds:

$validated = $validator->get();

When a chain only checks rules, get() returns the original value. Methods such as cleanString(), isDate(), transform(), and nested array validators can change the returned value.

Add error context

Most rules accept an error message followed by optional context data. The message is available through getMessage(), and the context is available through getData():

$validator = new Validator($input['email'] ?? null);

try {
    $email = $validator
        ->isEmail('Enter a valid email address', 'email')
        ->get();
} catch (ValidatorException $exception) {
    echo $exception->getData() . ': ' . $exception->getMessage();
}

The context can be any PHP value. Field names and nested paths are common choices.

Validate structured input

Use isArrayOfShape() to validate named fields:

$validator = new Validator([
    'name' => ' Linus ',
    'age' => 19,
]);

$profile = $validator
    ->isArrayOfShape([
        'name' => fn (Validator $field) => $field
            ->isString('Name must be a string', 'name')
            ->cleanString(),
        'age' => fn (Validator $field) => $field
            ->isInt('Age must be an integer', 'age')
            ->isGreaterThanOrEqual(13, 'You must be at least 13', 'age'),
    ])
    ->get();

The result contains the keys declared by the schema. Read Arrays and shapes before using schemas with optional or extra fields.

Troubleshooting

Class not found

Confirm that vendor/autoload.php is loaded and that the imports include the package's full current namespace:

use EinLinuus\PhpValidator\EinLinuus\PhpValidator\Validator;
use EinLinuus\PhpValidator\EinLinuus\PhpValidator\ValidatorException;

Empty exception messages

Rule error messages default to an empty string. Pass an explicit message to each rule that can fail.

A numeric string fails isInt()

isInt() only accepts PHP integers. For numeric strings such as "42", use isNumeric() or transform the value before applying integer rules.

Next steps