Skip to content

Commit a90b7ae

Browse files
committed
fix: Fixes for minContains and maxContains
1 parent d282db7 commit a90b7ae

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

src/JsonSchema/ConstraintError.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ class ConstraintError extends Enum
3939
public const INVALID_SCHEMA = 'invalidSchema';
4040
public const LENGTH_MAX = 'maxLength';
4141
public const LENGTH_MIN = 'minLength';
42+
public const MAX_CONTAINS = 'maxContains';
4243
public const MAXIMUM = 'maximum';
44+
public const MIN_CONTAINS = 'minContains';
4345
public const MIN_ITEMS = 'minItems';
4446
public const MINIMUM = 'minimum';
4547
public const MISSING_ERROR = 'missingError';
@@ -99,8 +101,10 @@ public function getMessage()
99101
self::LENGTH_MAX => 'Must be at most %d characters long',
100102
self::INVALID_SCHEMA => 'Schema is not valid',
101103
self::LENGTH_MIN => 'Must be at least %d characters long',
104+
self::MAX_CONTAINS => 'There must be a maximum of %d valid items in the array, %d found',
102105
self::MAX_ITEMS => 'There must be a maximum of %d items in the array, %d found',
103106
self::MAXIMUM => 'Must have a maximum value less than or equal to %d',
107+
self::MIN_CONTAINS => 'There must be a minimum of %d valid items in the array, %d found',
104108
self::MIN_ITEMS => 'There must be a minimum of %d items in the array, %d found',
105109
self::MINIMUM => 'Must have a minimum value greater than or equal to %d',
106110
self::MISSING_MAXIMUM => 'Use of exclusiveMaximum requires presence of maximum',

src/JsonSchema/Constraints/Drafts/Draft2019/ContainsConstraint.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,27 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
2828
return;
2929
}
3030

31-
$properties = [];
3231
if (!is_array($value)) {
3332
return;
3433
}
3534

36-
foreach ($value as $propertyName => $propertyValue) {
35+
$validElementCount = 0;
36+
foreach ($value as $propertyValue) {
3737
$schemaConstraint = $this->factory->createInstanceFor('schema');
3838

3939
$schemaConstraint->check($propertyValue, $schema->contains, $path, $i);
4040
if ($schemaConstraint->isValid()) {
41-
return;
41+
$validElementCount++;
4242
}
4343
}
4444

45-
$this->addError(ConstraintError::CONTAINS(), $path, ['contains' => $schema->contains]);
45+
if (property_exists($schema, 'maxContains') && $validElementCount > $schema->maxContains) {
46+
$this->addError(ConstraintError::MAX_CONTAINS(), $path, ['maxContains' => $schema->maxContains, 'count' => $validElementCount]);
47+
}
48+
49+
$minContains = property_exists($schema, 'minContains') ? $schema->minContains : 1;
50+
if ($validElementCount < $minContains) {
51+
$this->addError(ConstraintError::MIN_CONTAINS(), $path, ['minContains' => $minContains, 'count' => $validElementCount]);
52+
}
4653
}
4754
}

0 commit comments

Comments
 (0)