Skip to content

Primitives

use Innmind\Validation\Is;

$validate = Is::string();
use Innmind\Validation\Is;

$validate = Is::int();
use Innmind\Validation\Is;

$validate = Is::float();
use Innmind\Validation\Is;

$validate = Is::array();
use Innmind\Validation\Is;

$validate = Is::bool();
use Innmind\Validation\Is;

$validate = Is::null();

By default the error message will be Value is not of type {primitive}. You can change it via:

$validate = Is::string()->withFailure('Some error message');

Lists

This constraint validates the input is an array and all values are consecutive (1).

  1. No index value specified, be it ints or strings.
use Innmind\Validation\Is;

$validate = Is::list();

You can also validate that each value in the list is of a given type. Here's how to validate a list os strings:

use Innmind\Validation\Is;

$validate = Is::list(Is::string());

Specified value

This constraint makes sure the the input value is the expected one.

use Innmind\Validation\Is;

$validate = Is::value(42);

If you call the constraint with any other value than 42, the validation will fail. Of course you can specify any value of any type you wish.

Tip

This is especially useful to define discriminators when the input can be multiple shapes that are defined by a key.

use Innmind\Validation\Is;

$shapeA = Is::shape('discriminator', Is::value('a'))
    ->with('some-key', Is::string());
$shapeB = Is::shape('discriminator', Is::value('b'))
    ->with('other-key', Is::int());

$validate = $shapeA->or($shapeB);

If you can $validate with one of the following values it will succeed:

[
    'discriminator' => 'a',
    'some-key' => 'foo',
]
[
    'discriminator' => 'b',
    'other-key' => 42,
]

Otherwise it will fail for any other array shape.