From c79caf86a280a4fd1c0cd5b32e41b3d96fcdd040 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Thu, 27 Feb 2025 09:10:44 +0100 Subject: [PATCH 1/2] feat: include actual count in collection constraint errors https://github.com/jsonrainbow/json-schema/issues/501 --- src/JsonSchema/ConstraintError.php | 4 +- .../Constraints/CollectionConstraint.php | 4 +- tests/Constraints/MinItemsMaxItemsTest.php | 46 +++++++++++++++---- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/JsonSchema/ConstraintError.php b/src/JsonSchema/ConstraintError.php index 8b1c1450..c17cfeff 100644 --- a/src/JsonSchema/ConstraintError.php +++ b/src/JsonSchema/ConstraintError.php @@ -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', diff --git a/src/JsonSchema/Constraints/CollectionConstraint.php b/src/JsonSchema/Constraints/CollectionConstraint.php index 13e62a17..8d346a6f 100644 --- a/src/JsonSchema/Constraints/CollectionConstraint.php +++ b/src/JsonSchema/Constraints/CollectionConstraint.php @@ -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 diff --git a/tests/Constraints/MinItemsMaxItemsTest.php b/tests/Constraints/MinItemsMaxItemsTest.php index f07bcd6f..df6f0c75 100644 --- a/tests/Constraints/MinItemsMaxItemsTest.php +++ b/tests/Constraints/MinItemsMaxItemsTest.php @@ -9,6 +9,8 @@ namespace JsonSchema\Tests\Constraints; +use JsonSchema\Constraints\Constraint; + class MinItemsMaxItemsTest extends BaseTestCase { protected $validateSchema = true; @@ -16,27 +18,55 @@ class MinItemsMaxItemsTest extends BaseTestCase 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 + ]] ] ]; } From 6b84915ffd70dcf0acd0fbc360f7a9e428d406c6 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Thu, 27 Feb 2025 09:43:21 +0100 Subject: [PATCH 2/2] docs: add changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7490689c..2aa2fe1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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