Skip to content

Commit

Permalink
Merge pull request #641 from ergebnis/feature/items
Browse files Browse the repository at this point in the history
Enhancement: Normalize array values without schema definition
  • Loading branch information
localheinz committed Jan 21, 2022
2 parents a34c4d7 + 7115bfa commit b56423a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,7 @@ For a full diff see [`2.0.0...2.1.0`][2.0.0...2.1.0].
- Adjusted `SchemaNormalizer` to support `anyOf` ([#623]), by [@localheinz]
- Stopped checking whether `type` property in schema is set to `array` or `object` ([#632]), by [@localheinz]
- Adjusted `SchemaNormalizer` to normalize additional object properties ([#639]), by [@localheinz]
- Adjusted `SchemaNormalizer` to normalize array values for which schema does not declare item schema ([#641]), by [@localheinz]

## [`2.0.0`][2.0.0]

Expand Down Expand Up @@ -465,6 +466,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[#623]: https://github.com/ergebnis/json-normalizer/pull/623
[#632]: https://github.com/ergebnis/json-normalizer/pull/632
[#639]: https://github.com/ergebnis/json-normalizer/pull/639
[#641]: https://github.com/ergebnis/json-normalizer/pull/641

[@BackEndTea]: https://github.com/BackEndTea
[@dependabot]: https://github.com/dependabot
Expand Down
32 changes: 16 additions & 16 deletions src/SchemaNormalizer.php
Expand Up @@ -131,25 +131,25 @@ private function normalizeArray(
$schema,
);

/**
* @see https://json-schema.org/understanding-json-schema/reference/array.html#items
*/
if (!\property_exists($schema, 'items')) {
return $data;
}

$itemSchema = $schema->items;
$itemSchema = new \stdClass();

/**
* @see https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation
* @see https://json-schema.org/understanding-json-schema/reference/array.html#items
*/
if (\is_array($itemSchema)) {
return \array_map(function ($item, \stdClass $itemSchema) {
return $this->normalizeData(
$item,
$itemSchema,
);
}, $data, $itemSchema);
if (\property_exists($schema, 'items')) {
$itemSchema = $schema->items;

/**
* @see https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation
*/
if (\is_array($itemSchema)) {
return \array_map(function ($item, \stdClass $itemSchema) {
return $this->normalizeData(
$item,
$itemSchema,
);
}, $data, $itemSchema);
}
}

/**
Expand Down
Expand Up @@ -3,7 +3,7 @@
"bar",
"baz",
{
"foo": "bar",
"baz": "qux"
"baz": "qux",
"foo": "bar"
}
]
@@ -1,12 +1,12 @@
{
"bar": [
{
"foo": 9000,
"bar": [
"foo",
"bar",
"baz"
]
],
"foo": 9000
},
{
"foo": 9001
Expand Down

0 comments on commit b56423a

Please sign in to comment.