Skip to content

Commit

Permalink
feat: include actual count in collection constraint errors (#797)
Browse files Browse the repository at this point in the history
Includes the actual count in min/maxItems.

Fixes #501
  • Loading branch information
DannyvdSluijs authored Feb 27, 2025
1 parent 460c0a0 commit bb68276
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

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

## [6.2.0] - 2025-02-26
### Added
Expand Down
4 changes: 2 additions & 2 deletions src/JsonSchema/ConstraintError.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ public function getMessage()
self::LENGTH_MAX => 'Must be at most %d characters long',
self::INVALID_SCHEMA => 'Schema is not valid',
self::LENGTH_MIN => 'Must be at least %d characters long',
self::MAX_ITEMS => 'There must be a maximum of %d items in the array',
self::MAX_ITEMS => 'There must be a maximum of %d items in the array, %d found',
self::MAXIMUM => 'Must have a maximum value less than or equal to %d',
self::MIN_ITEMS => 'There must be a minimum of %d items in the array',
self::MIN_ITEMS => 'There must be a minimum of %d items in the array, %d found',
self::MINIMUM => 'Must have a minimum value greater than or equal to %d',
self::MISSING_MAXIMUM => 'Use of exclusiveMaximum requires presence of maximum',
self::MISSING_MINIMUM => 'Use of exclusiveMinimum requires presence of minimum',
Expand Down
4 changes: 2 additions & 2 deletions src/JsonSchema/Constraints/CollectionConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
{
// Verify minItems
if (isset($schema->minItems) && count($value) < $schema->minItems) {
$this->addError(ConstraintError::MIN_ITEMS(), $path, ['minItems' => $schema->minItems]);
$this->addError(ConstraintError::MIN_ITEMS(), $path, ['minItems' => $schema->minItems, 'found' => count($value)]);
}

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

// Verify uniqueItems
Expand Down
46 changes: 38 additions & 8 deletions tests/Constraints/MinItemsMaxItemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,64 @@

namespace JsonSchema\Tests\Constraints;

use JsonSchema\Constraints\Constraint;

class MinItemsMaxItemsTest extends BaseTestCase
{
protected $validateSchema = true;

public function getInvalidTests(): array
{
return [
[
'{
'Input violating minItems constraint' => [
'input' => '{
"value":[2]
}',
'{
'schema' => '{
"type":"object",
"properties":{
"value":{"type":"array","minItems":2,"maxItems":4}
}
}'
}',
'checkMode' => Constraint::CHECK_MODE_NORMAL,
[[
'property' => 'value',
'pointer' => '/value',
'message' => 'There must be a minimum of 2 items in the array, 1 found',
'constraint' => [
'name' => 'minItems',
'params' => [
'minItems' => 2,
'found' => 1
]
],
'context' => 1
]]
],
[
'{
'Input violating maxItems constraint' => [
'input' => '{
"value":[2,2,5,8,5]
}',
'{
'schema' => '{
"type":"object",
"properties":{
"value":{"type":"array","minItems":2,"maxItems":4}
}
}'
}',
'checkMode' => Constraint::CHECK_MODE_NORMAL,
[[
'property' => 'value',
'pointer' => '/value',
'message' => 'There must be a maximum of 4 items in the array, 5 found',
'constraint' => [
'name' => 'maxItems',
'params' => [
'maxItems' => 4,
'found' => 5
]
],
'context' => 1
]]
]
];
}
Expand Down

0 comments on commit bb68276

Please sign in to comment.