Skip to content

added combine handlers for min-maxProperites and min-maxItems #50

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

Merged
merged 1 commit into from
Aug 12, 2025
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
53 changes: 53 additions & 0 deletions src/error-handlers/array-range-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { getSchema } from "@hyperjump/json-schema/experimental";
import * as Schema from "@hyperjump/browser";
import * as Instance from "@hyperjump/json-schema/instance/experimental";

/**
* @import { ArrayConstraints } from "../localization.js"
* @import { ErrorHandler } from "../index.d.ts"
*/

/** @type ErrorHandler */
const arrayRangeHandler = async (normalizedErrors, instance, localization) => {
/** @type ArrayConstraints */
const constraints = {};

/** @type string[] */
const failedSchemaLocations = [];

for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/minItems"]) {
if (!normalizedErrors["https://json-schema.org/keyword/minItems"][schemaLocation]) {
failedSchemaLocations.push(schemaLocation);
}

const keyword = await getSchema(schemaLocation);
/** @type number */
const minItems = Schema.value(keyword);
constraints.minItems = Math.max(constraints.minItems ?? Number.MIN_VALUE, minItems);
}

for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/maxItems"]) {
if (!normalizedErrors["https://json-schema.org/keyword/maxItems"][schemaLocation]) {
failedSchemaLocations.push(schemaLocation);
}

const keyword = await getSchema(schemaLocation);
/** @type number */
const maxItems = Schema.value(keyword);
constraints.maxItems = Math.min(constraints.maxItems ?? Number.MAX_VALUE, maxItems);
}

if (failedSchemaLocations.length > 0) {
return [
{
message: localization.getArrayErrorMessage(constraints),
instanceLocation: Instance.uri(instance),
schemaLocation: failedSchemaLocations.length > 1 ? failedSchemaLocations : failedSchemaLocations[0]
}
];
}

return [];
};

export default arrayRangeHandler;
30 changes: 0 additions & 30 deletions src/error-handlers/maxItems.js

This file was deleted.

30 changes: 0 additions & 30 deletions src/error-handlers/maxProperties.js

This file was deleted.

30 changes: 0 additions & 30 deletions src/error-handlers/minItems.js

This file was deleted.

30 changes: 0 additions & 30 deletions src/error-handlers/minProperties.js

This file was deleted.

53 changes: 53 additions & 0 deletions src/error-handlers/properties-range-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { getSchema } from "@hyperjump/json-schema/experimental";
import * as Schema from "@hyperjump/browser";
import * as Instance from "@hyperjump/json-schema/instance/experimental";

/**
* @import { PropertiesConstraints } from "../localization.js"
* @import { ErrorHandler } from "../index.d.ts"
*/

/** @type ErrorHandler */
const propertiesRangeHandler = async (normalizedErrors, instance, localization) => {
/** @type PropertiesConstraints */
const constraints = {};

/** @type string[] */
const failedSchemaLocations = [];

for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/minProperties"]) {
if (!normalizedErrors["https://json-schema.org/keyword/minProperties"][schemaLocation]) {
failedSchemaLocations.push(schemaLocation);
}

const keyword = await getSchema(schemaLocation);
/** @type number */
const minProperties = Schema.value(keyword);
constraints.minProperties = Math.max(constraints.minProperties ?? Number.MIN_VALUE, minProperties);
}

for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/maxProperties"]) {
if (!normalizedErrors["https://json-schema.org/keyword/maxProperties"][schemaLocation]) {
failedSchemaLocations.push(schemaLocation);
}

const keyword = await getSchema(schemaLocation);
/** @type number */
const maxProperties = Schema.value(keyword);
constraints.maxProperties = Math.min(constraints.maxProperties ?? Number.MAX_VALUE, maxProperties);
}

if (failedSchemaLocations.length > 0) {
return [
{
message: localization.getPropertiesErrorMessage(constraints),
instanceLocation: Instance.uri(instance),
schemaLocation: failedSchemaLocations.length > 1 ? failedSchemaLocations : failedSchemaLocations[0]
}
];
}

return [];
};

export default propertiesRangeHandler;
12 changes: 4 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,12 @@ import uniqueItems from "./normalization-handlers/uniqueItems.js";
// Error Handlers
import anyOfErrorHandler from "./error-handlers/anyOf.js";
import additionalPropertiesErrorHandler from "./error-handlers/additionalProperties.js";
import arrayRangeErrorHandler from "./error-handlers/array-range-handler.js";
import constErrorHandler from "./error-handlers/const.js";
import containsErrorHandler from "./error-handlers/contains.js";
import dependentRequiredErrorHandler from "./error-handlers/dependentRequired.js";
import enumErrorHandler from "./error-handlers/enum.js";
import formatErrorHandler from "./error-handlers/format.js";
import maxItemsErrorHandler from "./error-handlers/maxItems.js";
import minItemsErrorHandler from "./error-handlers/minItems.js";
import maxPropertiesErrorHandler from "./error-handlers/maxProperties.js";
import minPropertiesErrorHandler from "./error-handlers/minProperties.js";
import multipleOfErrorHandler from "./error-handlers/multipleOf.js";
import notErrorHandler from "./error-handlers/not.js";
import numberRangeHandler from "./error-handlers/number-range-handler.js";
Expand All @@ -63,6 +60,7 @@ import typeErrorHandler from "./error-handlers/type.js";
import uniqueItemsErrorHandler from "./error-handlers/uniqueItems.js";
import stringErrorHandler from "./error-handlers/string-handler.js";
import patternErrorHandler from "./error-handlers/pattern.js";
import propertiesRangeHandler from "./error-handlers/properties-range-handler.js";

/**
* @import { betterJsonSchemaErrors } from "./index.d.ts"
Expand Down Expand Up @@ -122,10 +120,7 @@ addErrorHandler(containsErrorHandler);
addErrorHandler(dependentRequiredErrorHandler);
addErrorHandler(enumErrorHandler);
addErrorHandler(formatErrorHandler);
addErrorHandler(maxItemsErrorHandler);
addErrorHandler(minItemsErrorHandler);
addErrorHandler(maxPropertiesErrorHandler);
addErrorHandler(minPropertiesErrorHandler);
addErrorHandler(arrayRangeErrorHandler);
addErrorHandler(multipleOfErrorHandler);
addErrorHandler(notErrorHandler);
addErrorHandler(numberRangeHandler);
Expand All @@ -134,6 +129,7 @@ addErrorHandler(typeErrorHandler);
addErrorHandler(uniqueItemsErrorHandler);
addErrorHandler(stringErrorHandler);
addErrorHandler(patternErrorHandler);
addErrorHandler(propertiesRangeHandler);

export { setNormalizationHandler } from "./normalized-output.js";
export { addErrorHandler } from "./error-handling.js";
79 changes: 75 additions & 4 deletions src/keyword-error-message.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ describe("Error messages", async () => {
expect(result.errors).to.eql([{
schemaLocation: "https://example.com/main#/maxProperties",
instanceLocation: "#",
message: localization.getMaxPropertiesErrorMessage(2)
message: localization.getPropertiesErrorMessage({ maxProperties: 2 })
}]);
});

Expand Down Expand Up @@ -424,7 +424,43 @@ describe("Error messages", async () => {
expect(result.errors).to.eql([{
schemaLocation: "https://example.com/main#/minProperties",
instanceLocation: "#",
message: localization.getMinPropertiesErrorMessage(2)
message: localization.getPropertiesErrorMessage({ minProperties: 2 })
}]);
});

test("max-min Properties", async () => {
registerSchema({
$schema: "https://json-schema.org/draft/2020-12/schema",
maxProperties: 3,
minProperties: 2

}, schemaUri);

const instance = { foo: 1 };

/** @type OutputFormat */
const output = {
valid: false,
errors: [
{
absoluteKeywordLocation: "https://example.com/main#/maxProperties",
instanceLocation: "#"
},
{
absoluteKeywordLocation: "https://example.com/main#/minProperties",
instanceLocation: "#"
}
]
};

const result = await betterJsonSchemaErrors(output, schemaUri, instance);
expect(result.errors).to.eql([{
schemaLocation: [
"https://example.com/main#/minProperties",
"https://example.com/main#/maxProperties"
],
instanceLocation: "#",
message: localization.getPropertiesErrorMessage({ maxProperties: 3, minProperties: 2 })
}]);
});

Expand Down Expand Up @@ -507,7 +543,7 @@ describe("Error messages", async () => {
expect(result.errors).to.eql([{
schemaLocation: "https://example.com/main#/maxItems",
instanceLocation: "#",
message: localization.getMaxItemsErrorMessage(3)
message: localization.getArrayErrorMessage({ maxItems: 3 })
}]);
});

Expand All @@ -534,7 +570,42 @@ describe("Error messages", async () => {
expect(result.errors).to.eql([{
schemaLocation: "https://example.com/main#/minItems",
instanceLocation: "#",
message: localization.getMinItemsErrorMessage(3)
message: localization.getArrayErrorMessage({ minItems: 3 })
}]);
});

test("minItems and maxItems", async () => {
registerSchema({
$schema: "https://json-schema.org/draft/2020-12/schema",
minItems: 3,
maxItems: 4
}, schemaUri);

const instance = [1, 3];

/** @type OutputFormat */
const output = {
valid: false,
errors: [
{
absoluteKeywordLocation: "https://example.com/main#/minItems",
instanceLocation: "#"
},
{
absoluteKeywordLocation: "https://example.com/main#/maxItems",
instanceLocation: "#"
}
]
};

const result = await betterJsonSchemaErrors(output, schemaUri, instance);
expect(result.errors).to.eql([{
schemaLocation: [
"https://example.com/main#/minItems",
"https://example.com/main#/maxItems"
],
instanceLocation: "#",
message: localization.getArrayErrorMessage({ minItems: 3, maxItems: 4 })
}]);
});

Expand Down
Loading