Skip to content

feat: include actual count in collection constraint errors #797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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