Verja¶
Verja (Old Norse: defender) is a PHP validation library for external input — forms, JSON bodies, query parameters, and any other data you don't control.
Core concept: Filter → Validate¶
Most validation libraries only check constraints. Verja makes two explicit steps out of it:
- Filters transform raw input into the right type — trim whitespace, cast strings to
integers, convert
"true"totrue. - Validators check constraints on the already-filtered value.
This means a validator never has to handle both "42" and 42. It always sees clean data.
// Raw input: ["age" => " 42 "]
// Integer filter: " 42 " → trim → "42" → cast → 42
// Between validator: checks 42 is within 0–150
$gate = (new Verja\Gate())
->int('age', 'between:0:150');
$result = $gate->validate($_POST);
// $result->data['age'] === 42 (int, not string)
Gate hierarchy¶
Verja provides three gate types that map to real data shapes:
| Type | Use case |
|---|---|
Gate |
Structured data — objects and arrays with named keys |
PropertyGate |
A single scalar value |
ArrayGate |
A list where every element follows the same rules |
Gates compose: Gate::object() accepts a nested Gate, Gate::array() wraps an ArrayGate.
Errors from nested structures are automatically flattened to dot-notation paths
(author.email, tags.1) in $result->errorMap.
Installation¶
Requires PHP 8.2 or later.
Upgrading from v1? See the migration guide.