Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jsonrainbow/json-schema
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 6.2.0
Choose a base ref
...
head repository: jsonrainbow/json-schema
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 6.2.1
Choose a head ref
  • 3 commits
  • 6 files changed
  • 1 contributor

Commits on Feb 27, 2025

  1. feat: include actual count in collection constraint errors (#797)

    Includes the actual count in min/maxItems.
    
    Fixes #501
    DannyvdSluijs authored Feb 27, 2025
    Copy the full SHA
    bb68276 View commit details

Commits on Mar 6, 2025

  1. fix: allow items: true to pass validation (#801)

    Fixes #799
    DannyvdSluijs authored Mar 6, 2025
    Copy the full SHA
    949a4d5 View commit details
  2. docs: release 6.2.1

    DannyvdSluijs committed Mar 6, 2025
    Copy the full SHA
    dff04e3 View commit details
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [6.2.1] - 2025-03-06
### 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))

## [6.2.0] - 2025-02-26
### Added
- Welcome first time contributors ([#782](https://github.com/jsonrainbow/json-schema/pull/782))
20 changes: 0 additions & 20 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions src/JsonSchema/ConstraintError.php
Original file line number Diff line number Diff line change
@@ -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',
17 changes: 11 additions & 6 deletions src/JsonSchema/Constraints/CollectionConstraint.php
Original file line number Diff line number Diff line change
@@ -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
@@ -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,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) {
12 changes: 11 additions & 1 deletion tests/Constraints/ArraysTest.php
Original file line number Diff line number Diff line change
@@ -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
,
],
];
}
}
46 changes: 38 additions & 8 deletions tests/Constraints/MinItemsMaxItemsTest.php
Original file line number Diff line number Diff line change
@@ -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
]]
]
];
}