function CollectionConstraint::check
Overrides ConstraintInterface::check
File
-
vendor/
justinrainbow/ json-schema/ src/ JsonSchema/ Constraints/ CollectionConstraint.php, line 25
Class
- CollectionConstraint
- The CollectionConstraint Constraints, validates an array against a given schema
Namespace
JsonSchema\ConstraintsCode
public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null) {
// Verify minItems
if (isset($schema->minItems) && count($value) < $schema->minItems) {
$this->addError($path, 'There must be a minimum of ' . $schema->minItems . ' items in the array', 'minItems', array(
'minItems' => $schema->minItems,
));
}
// Verify maxItems
if (isset($schema->maxItems) && count($value) > $schema->maxItems) {
$this->addError($path, 'There must be a maximum of ' . $schema->maxItems . ' items in the array', 'maxItems', array(
'maxItems' => $schema->maxItems,
));
}
// Verify uniqueItems
if (isset($schema->uniqueItems) && $schema->uniqueItems) {
$unique = $value;
if (is_array($value) && count($value)) {
$unique = array_map(function ($e) {
return var_export($e, true);
}, $value);
}
if (count(array_unique($unique)) != count($value)) {
$this->addError($path, 'There are no duplicates allowed in the array', 'uniqueItems');
}
}
// Verify items
if (isset($schema->items)) {
$this->validateItems($value, $schema, $path, $i);
}
}