Skip to content
This repository was archived by the owner on Jul 6, 2024. It is now read-only.

Latest commit

 

History

History
43 lines (35 loc) · 825 Bytes

File metadata and controls

43 lines (35 loc) · 825 Bytes

MapConstraint

<?php

use Chubbyphp\Validation\Constraint\MapConstraint;
use Chubbyphp\Validation\Constraint\NotBlankConstraint;
use Chubbyphp\Validation\Constraint\TypeConstraint;
use Chubbyphp\Validation\ValidatorContextInterface;

$constraint = new MapConstraint([
    'name' => new TypeConstraint('string'),
]);

// or
$constraint = new MapConstraint([
    'name' => [
        new TypeConstraint('string'),
        new NotBlankConstraint(),
    ],
]);


$values = [
    'name' => 1,
];

/** @var ValidatorContextInterface $context */
$context = ...;

$errors = $constraint->validate(
    'path.to.property',
    $values,
    $context
);
// [
//     new Error(
//         'path.to.property.name',
//         'constraint.type.invalidtype',
//         ['type' => 'int', 'wishedType' => 'string']
//     )
// ];