Skip to content

fix: allow items: true to pass validation #801

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 4 commits into from
Mar 6, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
20 changes: 0 additions & 20 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions src/JsonSchema/Constraints/CollectionConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -65,6 +62,14 @@ 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_null($schema) || !isset($schema->items)) {
return;
}

if ($schema->items === true) {
return;
}

if (is_object($schema->items)) {
// just one type definition for the whole array
foreach ($value as $k => &$v) {
Expand Down
12 changes: 11 additions & 1 deletion tests/Constraints/ArraysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,17 @@ public function getValidTests(): array
}
}
}'
]
],
'items: true passes validation' => [
'input' => <<<JSON
[1, 1.2, "12"]
JSON
,
'schema' => <<<JSON
{ "type": "array", "items": true }
JSON
,
],
];
}
}