Skip to content
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

Enhancement: Normalize array values without schema definition #641

Merged
merged 1 commit into from Jan 21, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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