From 276dd7b0ba9a8868f350060d19740bb9b795ec79 Mon Sep 17 00:00:00 2001 From: Padmashree06 Date: Thu, 29 Jan 2026 01:49:22 +0530 Subject: [PATCH 1/2] Combine error messages for maxItems and minItems --- src/error-handlers/maxItems.js | 13 +++++++++++-- src/error-handlers/minItems.js | 13 +++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/error-handlers/maxItems.js b/src/error-handlers/maxItems.js index c13283f..a821a34 100644 --- a/src/error-handlers/maxItems.js +++ b/src/error-handlers/maxItems.js @@ -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]) { @@ -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] }); } diff --git a/src/error-handlers/minItems.js b/src/error-handlers/minItems.js index f4befe0..13da179 100644 --- a/src/error-handlers/minItems.js +++ b/src/error-handlers/minItems.js @@ -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]) { @@ -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] }); } From 21b752ad183f015e3c6440160f5cabe25997e324 Mon Sep 17 00:00:00 2001 From: Padmashree06 Date: Thu, 29 Jan 2026 23:04:16 +0530 Subject: [PATCH 2/2] Add tests for maxItems and minItems --- src/test-suite/tests/maxItems.json | 17 +++++++++++++++++ src/test-suite/tests/minItems.json | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/test-suite/tests/maxItems.json b/src/test-suite/tests/maxItems.json index 816f8c7..64b755c 100644 --- a/src/test-suite/tests/maxItems.json +++ b/src/test-suite/tests/maxItems.json @@ -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"] + } + ] } ] } diff --git a/src/test-suite/tests/minItems.json b/src/test-suite/tests/minItems.json index b91b4e2..bd9a45e 100644 --- a/src/test-suite/tests/minItems.json +++ b/src/test-suite/tests/minItems.json @@ -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"] + } + ] } ] }