From cafb390e5d68e38124c9d0a61089b31dde1a6e02 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Thu, 6 Mar 2025 22:15:03 +0100 Subject: [PATCH 1/4] fix: allow items: true to pass validation --- src/JsonSchema/Constraints/CollectionConstraint.php | 4 ++++ tests/Constraints/ArraysTest.php | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/JsonSchema/Constraints/CollectionConstraint.php b/src/JsonSchema/Constraints/CollectionConstraint.php index 8d346a6f..47aa93dc 100644 --- a/src/JsonSchema/Constraints/CollectionConstraint.php +++ b/src/JsonSchema/Constraints/CollectionConstraint.php @@ -65,6 +65,10 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n */ protected function validateItems(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void { + if (is_bool($schema->items) && $schema->items === true) { + return; + } + if (is_object($schema->items)) { // just one type definition for the whole array foreach ($value as $k => &$v) { diff --git a/tests/Constraints/ArraysTest.php b/tests/Constraints/ArraysTest.php index bb11a091..9c63982e 100644 --- a/tests/Constraints/ArraysTest.php +++ b/tests/Constraints/ArraysTest.php @@ -271,7 +271,17 @@ public function getValidTests(): array } } }' - ] + ], + 'items: true passes validation' => [ + 'input' => << << Date: Thu, 6 Mar 2025 22:16:49 +0100 Subject: [PATCH 2/4] docs: add changelog entry --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2aa2fe1f..577dabde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ 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] +### Fixed +- allow items: true to pass validation ([#801](https://github.com/jsonrainbow/json-schema/pull/801)) + ### Changed - Include actual count in collection constraint errors ([#797](https://github.com/jsonrainbow/json-schema/pull/797)) From b04b207b036b6ec95e17cf515840dc4829aae40e Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Thu, 6 Mar 2025 22:22:02 +0100 Subject: [PATCH 3/4] refactor: move condition check into function --- phpstan-baseline.neon | 20 ------------------- .../Constraints/CollectionConstraint.php | 11 +++++----- 2 files changed, 6 insertions(+), 25 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index d4116654..aaa7831a 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -30,31 +30,11 @@ parameters: count: 1 path: src/JsonSchema/Constraints/BaseConstraint.php - - - message: "#^Cannot access property \\$additionalItems on stdClass\\|null\\.$#" - count: 3 - path: src/JsonSchema/Constraints/CollectionConstraint.php - - - - message: "#^Cannot access property \\$items on stdClass\\|null\\.$#" - count: 6 - path: src/JsonSchema/Constraints/CollectionConstraint.php - - message: "#^Method JsonSchema\\\\Constraints\\\\CollectionConstraint\\:\\:validateItems\\(\\) has parameter \\$value with no value type specified in iterable type array\\.$#" count: 1 path: src/JsonSchema/Constraints/CollectionConstraint.php - - - message: "#^Parameter \\#1 \\$object_or_class of function property_exists expects object\\|string, stdClass\\|null given\\.$#" - count: 1 - path: src/JsonSchema/Constraints/CollectionConstraint.php - - - - message: "#^Parameter \\#2 \\$schema of method JsonSchema\\\\Constraints\\\\CollectionConstraint\\:\\:validateItems\\(\\) expects stdClass\\|null, object given\\.$#" - count: 1 - path: src/JsonSchema/Constraints/CollectionConstraint.php - - message: "#^Method JsonSchema\\\\Constraints\\\\Constraint\\:\\:checkObject\\(\\) has parameter \\$appliedDefaults with no type specified\\.$#" count: 1 diff --git a/src/JsonSchema/Constraints/CollectionConstraint.php b/src/JsonSchema/Constraints/CollectionConstraint.php index 47aa93dc..0c73d828 100644 --- a/src/JsonSchema/Constraints/CollectionConstraint.php +++ b/src/JsonSchema/Constraints/CollectionConstraint.php @@ -50,10 +50,7 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n } } - // Verify items - if (isset($schema->items)) { - $this->validateItems($value, $schema, $path, $i); - } + $this->validateItems($value, $schema, $path, $i); } /** @@ -65,7 +62,11 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n */ protected function validateItems(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void { - if (is_bool($schema->items) && $schema->items === true) { + if (\is_null($schema) || !isset($schema->items)) { + return; + } + + if ($schema->items === true) { return; } From f0d91fdfa863df1716b623eb76f9736f1cf38550 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Thu, 6 Mar 2025 22:22:37 +0100 Subject: [PATCH 4/4] style: correct code style violation --- src/JsonSchema/Constraints/CollectionConstraint.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JsonSchema/Constraints/CollectionConstraint.php b/src/JsonSchema/Constraints/CollectionConstraint.php index 0c73d828..da0e7150 100644 --- a/src/JsonSchema/Constraints/CollectionConstraint.php +++ b/src/JsonSchema/Constraints/CollectionConstraint.php @@ -50,7 +50,7 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n } } - $this->validateItems($value, $schema, $path, $i); + $this->validateItems($value, $schema, $path, $i); } /**