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

Latest commit

 

History

History
74 lines (64 loc) · 1.29 KB

CountConstraint.md

File metadata and controls

74 lines (64 loc) · 1.29 KB

CountConstraint

<?php

use Chubbyphp\Validation\Constraint\CountConstraint;
use Chubbyphp\Validation\ValidatorContextInterface;

$constraint = new CountConstraint(1, 2);

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

// Use NotNullConstraint to prevent null
$errors = $constraint->validate(
    'path.to.property',
    null,
    $context
);
// [];

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

$errors = $constraint->validate(
    'path.to.property',
    [],
    $context
);
// [
//     new Error(
//         'path.to.property',
//         'constraint.count.outofrange',
//         ['count' => 0, 'min' => 1, 'max' => 2]
//     )
// ];

$errors = $constraint->validate(
    'path.to.property',
    ['value'],
    $context
);
// [];

$errors = $constraint->validate(
    'path.to.property',
    ['value', 'value'],
    $context
);
// [];

$errors = $constraint->validate(
    'path.to.property',
    ['value', 'value', 'value'],
    $context
);
// [
//     new Error(
//         'path.to.property',
//         'constraint.count.outofrange',
//         ['count' => 3, 'min' => 1, 'max' => 2]
//     )
// ];