Skip to content

Commit bb68276

Browse files
feat: include actual count in collection constraint errors (#797)
Includes the actual count in min/maxItems. Fixes #501
1 parent 460c0a0 commit bb68276

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
### Changed
10+
- Include actual count in collection constraint errors ([#797](https://github.com/jsonrainbow/json-schema/pull/797))
911

1012
## [6.2.0] - 2025-02-26
1113
### Added

src/JsonSchema/ConstraintError.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ public function getMessage()
8888
self::LENGTH_MAX => 'Must be at most %d characters long',
8989
self::INVALID_SCHEMA => 'Schema is not valid',
9090
self::LENGTH_MIN => 'Must be at least %d characters long',
91-
self::MAX_ITEMS => 'There must be a maximum of %d items in the array',
91+
self::MAX_ITEMS => 'There must be a maximum of %d items in the array, %d found',
9292
self::MAXIMUM => 'Must have a maximum value less than or equal to %d',
93-
self::MIN_ITEMS => 'There must be a minimum of %d items in the array',
93+
self::MIN_ITEMS => 'There must be a minimum of %d items in the array, %d found',
9494
self::MINIMUM => 'Must have a minimum value greater than or equal to %d',
9595
self::MISSING_MAXIMUM => 'Use of exclusiveMaximum requires presence of maximum',
9696
self::MISSING_MINIMUM => 'Use of exclusiveMinimum requires presence of minimum',

src/JsonSchema/Constraints/CollectionConstraint.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
2929
{
3030
// Verify minItems
3131
if (isset($schema->minItems) && count($value) < $schema->minItems) {
32-
$this->addError(ConstraintError::MIN_ITEMS(), $path, ['minItems' => $schema->minItems]);
32+
$this->addError(ConstraintError::MIN_ITEMS(), $path, ['minItems' => $schema->minItems, 'found' => count($value)]);
3333
}
3434

3535
// Verify maxItems
3636
if (isset($schema->maxItems) && count($value) > $schema->maxItems) {
37-
$this->addError(ConstraintError::MAX_ITEMS(), $path, ['maxItems' => $schema->maxItems]);
37+
$this->addError(ConstraintError::MAX_ITEMS(), $path, ['maxItems' => $schema->maxItems, 'found' => count($value)]);
3838
}
3939

4040
// Verify uniqueItems

tests/Constraints/MinItemsMaxItemsTest.php

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,64 @@
99

1010
namespace JsonSchema\Tests\Constraints;
1111

12+
use JsonSchema\Constraints\Constraint;
13+
1214
class MinItemsMaxItemsTest extends BaseTestCase
1315
{
1416
protected $validateSchema = true;
1517

1618
public function getInvalidTests(): array
1719
{
1820
return [
19-
[
20-
'{
21+
'Input violating minItems constraint' => [
22+
'input' => '{
2123
"value":[2]
2224
}',
23-
'{
25+
'schema' => '{
2426
"type":"object",
2527
"properties":{
2628
"value":{"type":"array","minItems":2,"maxItems":4}
2729
}
28-
}'
30+
}',
31+
'checkMode' => Constraint::CHECK_MODE_NORMAL,
32+
[[
33+
'property' => 'value',
34+
'pointer' => '/value',
35+
'message' => 'There must be a minimum of 2 items in the array, 1 found',
36+
'constraint' => [
37+
'name' => 'minItems',
38+
'params' => [
39+
'minItems' => 2,
40+
'found' => 1
41+
]
42+
],
43+
'context' => 1
44+
]]
2945
],
30-
[
31-
'{
46+
'Input violating maxItems constraint' => [
47+
'input' => '{
3248
"value":[2,2,5,8,5]
3349
}',
34-
'{
50+
'schema' => '{
3551
"type":"object",
3652
"properties":{
3753
"value":{"type":"array","minItems":2,"maxItems":4}
3854
}
39-
}'
55+
}',
56+
'checkMode' => Constraint::CHECK_MODE_NORMAL,
57+
[[
58+
'property' => 'value',
59+
'pointer' => '/value',
60+
'message' => 'There must be a maximum of 4 items in the array, 5 found',
61+
'constraint' => [
62+
'name' => 'maxItems',
63+
'params' => [
64+
'maxItems' => 4,
65+
'found' => 5
66+
]
67+
],
68+
'context' => 1
69+
]]
4070
]
4171
];
4272
}

0 commit comments

Comments
 (0)