emonkak/validation

A type-based validation library

v1.0.1 2020-06-03 16:23 UTC

This package is auto-updated.

Last update: 2024-04-29 02:48:54 UTC


README

Build Status Coverage Status

Example

use Emonkak\Validation\Types;
use Emonkak\Validation\Validator;

$validator = new Validator([
    'foo' => Types::oneOfType([Types::int(), Types::bool()]),
    'bar' => Types::string(),
    'baz' => Types::bool(),
    'qux' => Types::any(),
    'quux' => Types::arrayOf(Types::string()),
    'foobar' => Types::string()->isOptional(),
    'piyo' => Types::oneOf(['foo', 'bar']),
    'puyo' => Types::shape('Puyo', ['foo' => Types::string()]),
    'payo' => Types::dateTime(),
]);

$errors = $validator->validate([
    'foo' => 'foo',
    'bar' => '123',
    'baz' => 'true',
    'qux' => null,
    'quux' => ['1', '2'],
    'puyo' => ['foo' => 123],
    'payo' => '2000-01-01 00:00:00'
]);

foreach ($errors->getErrors() as $key => $errors) {
    echo $key, ': ', implode(' ', $errors), PHP_EOL;
}

// OUTPUT:
// foo: The property `foo` must be `integer|boolean`, got `string`.
// qux: The property `qux` must be `any`, got `NULL`.
// piyo: The property `piyo` must be `"foo"|"bar"`, got `NULL`.
// puyo.foo: The property `puyo.foo` must be `string`, got `integer`.

Licence

MIT Licence