Skip to content
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
13 changes: 11 additions & 2 deletions src/error-handlers/maxItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import * as Instance from "@hyperjump/json-schema/instance/experimental";
const maxItemsErrorHandler = async (normalizedErrors, instance, localization) => {
/** @type ErrorObject[] */
const errors = [];
let lowestMaxItems = Infinity;
let effectiveSchemaLocation = "";

for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/maxItems"]) {
if (normalizedErrors["https://json-schema.org/keyword/maxItems"][schemaLocation]) {
Expand All @@ -19,10 +21,17 @@ const maxItemsErrorHandler = async (normalizedErrors, instance, localization) =>
const keyword = await getSchema(schemaLocation);
const maxItems = /** @type number */ (Schema.value(keyword));

if (maxItems < lowestMaxItems) {
lowestMaxItems = maxItems;
effectiveSchemaLocation = schemaLocation;
}
}

if (lowestMaxItems != Infinity) {
errors.push({
message: localization.getMaxItemsErrorMessage(maxItems),
message: localization.getMaxItemsErrorMessage(lowestMaxItems),
instanceLocation: Instance.uri(instance),
schemaLocations: [schemaLocation]
schemaLocations: [effectiveSchemaLocation]
});
}

Expand Down
13 changes: 11 additions & 2 deletions src/error-handlers/minItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import * as Instance from "@hyperjump/json-schema/instance/experimental";
const minItemsErrorHandler = async (normalizedErrors, instance, localization) => {
/** @type ErrorObject[] */
const errors = [];
let highestMinItem = 0;
let effectiveSchemaLocation = "";

for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/minItems"]) {
if (normalizedErrors["https://json-schema.org/keyword/minItems"][schemaLocation]) {
Expand All @@ -19,10 +21,17 @@ const minItemsErrorHandler = async (normalizedErrors, instance, localization) =>
const keyword = await getSchema(schemaLocation);
const minItems = /** @type number */ (Schema.value(keyword));

if (minItems > highestMinItem) {
highestMinItem = minItems;
effectiveSchemaLocation = schemaLocation;
}
}

if (highestMinItem != 0) {
errors.push({
message: localization.getMinItemsErrorMessage(minItems),
message: localization.getMinItemsErrorMessage(highestMinItem),
instanceLocation: Instance.uri(instance),
schemaLocations: [schemaLocation]
schemaLocations: [effectiveSchemaLocation]
});
}

Expand Down
17 changes: 17 additions & 0 deletions src/test-suite/tests/maxItems.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@
},
"instance": ["foo"],
"errors": []
},
{
"description": "maxItems with multiple constraints (lowest value considered)",
"schema": {
"allOf": [{ "maxItems": 2 }, { "maxItems": 1 }]
},
"instance": ["foo", "bar", "word"],
"errors": [
{
"messageId": "maxItems-message",
"messageParams": {
"maxItems": "1"
},
"instanceLocation": "#",
"schemaLocations": ["#/allOf/1/maxItems"]
}
]
}
]
}
17 changes: 17 additions & 0 deletions src/test-suite/tests/minItems.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@
},
"instance": ["foo"],
"errors": []
},
{
"description": "minItems with multiple constraints (highest value is considered)",
"schema": {
"allOf": [{ "minItems": 4 }, { "minItems": 3 }]
},
"instance": ["foo", "bar"],
"errors": [
{
"messageId": "minItems-message",
"messageParams": {
"minItems": "4"
},
"instanceLocation": "#",
"schemaLocations": ["#/allOf/0/minItems"]
}
]
}
]
}