Skip to content

Commit 4b22392

Browse files
authored
Combine error messages for maxItems and minItems (#152)
* Combine error messages for maxItems and minItems * Add tests for maxItems and minItems
1 parent 8f83b4b commit 4b22392

4 files changed

Lines changed: 56 additions & 4 deletions

File tree

src/error-handlers/maxItems.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import * as Instance from "@hyperjump/json-schema/instance/experimental";
1010
const maxItemsErrorHandler = async (normalizedErrors, instance, localization) => {
1111
/** @type ErrorObject[] */
1212
const errors = [];
13+
let lowestMaxItems = Infinity;
14+
let effectiveSchemaLocation = "";
1315

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

24+
if (maxItems < lowestMaxItems) {
25+
lowestMaxItems = maxItems;
26+
effectiveSchemaLocation = schemaLocation;
27+
}
28+
}
29+
30+
if (lowestMaxItems != Infinity) {
2231
errors.push({
23-
message: localization.getMaxItemsErrorMessage(maxItems),
32+
message: localization.getMaxItemsErrorMessage(lowestMaxItems),
2433
instanceLocation: Instance.uri(instance),
25-
schemaLocations: [schemaLocation]
34+
schemaLocations: [effectiveSchemaLocation]
2635
});
2736
}
2837

src/error-handlers/minItems.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import * as Instance from "@hyperjump/json-schema/instance/experimental";
1010
const minItemsErrorHandler = async (normalizedErrors, instance, localization) => {
1111
/** @type ErrorObject[] */
1212
const errors = [];
13+
let highestMinItem = 0;
14+
let effectiveSchemaLocation = "";
1315

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

24+
if (minItems > highestMinItem) {
25+
highestMinItem = minItems;
26+
effectiveSchemaLocation = schemaLocation;
27+
}
28+
}
29+
30+
if (highestMinItem != 0) {
2231
errors.push({
23-
message: localization.getMinItemsErrorMessage(minItems),
32+
message: localization.getMinItemsErrorMessage(highestMinItem),
2433
instanceLocation: Instance.uri(instance),
25-
schemaLocations: [schemaLocation]
34+
schemaLocations: [effectiveSchemaLocation]
2635
});
2736
}
2837

src/test-suite/tests/maxItems.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@
2727
},
2828
"instance": ["foo"],
2929
"errors": []
30+
},
31+
{
32+
"description": "maxItems with multiple constraints (lowest value considered)",
33+
"schema": {
34+
"allOf": [{ "maxItems": 2 }, { "maxItems": 1 }]
35+
},
36+
"instance": ["foo", "bar", "word"],
37+
"errors": [
38+
{
39+
"messageId": "maxItems-message",
40+
"messageParams": {
41+
"maxItems": "1"
42+
},
43+
"instanceLocation": "#",
44+
"schemaLocations": ["#/allOf/1/maxItems"]
45+
}
46+
]
3047
}
3148
]
3249
}

src/test-suite/tests/minItems.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@
2727
},
2828
"instance": ["foo"],
2929
"errors": []
30+
},
31+
{
32+
"description": "minItems with multiple constraints (highest value is considered)",
33+
"schema": {
34+
"allOf": [{ "minItems": 4 }, { "minItems": 3 }]
35+
},
36+
"instance": ["foo", "bar"],
37+
"errors": [
38+
{
39+
"messageId": "minItems-message",
40+
"messageParams": {
41+
"minItems": "4"
42+
},
43+
"instanceLocation": "#",
44+
"schemaLocations": ["#/allOf/0/minItems"]
45+
}
46+
]
3047
}
3148
]
3249
}

0 commit comments

Comments
 (0)