Skip to content
Open
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
32 changes: 0 additions & 32 deletions src/error-handlers/const.js

This file was deleted.

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

/**
* @import { ErrorHandler, ErrorObject, Json } from "../index.d.ts"
*/

/**
* @typedef {{
* allowedValues: Json[];
* schemaLocation: string;
* }} Constraint
*/

/** @type {(a: Json, b: Json) => boolean} */
const jsonEqual = (a, b) => jsonStringify(a) === jsonStringify(b);
Comment on lines +17 to +18
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a test where that would fail if we didn't use the deterministic stringify approach.

Also, as a style rule, I like to put helper functions at the bottom.


/** @type {ErrorHandler} */
const constAndEnumErrorHandler = async (normalizedErrors, instance, localization) => {
/** @type {ErrorObject[]} */
const errors = [];

/** @type {Constraint[]} */
const constraints = [];
let hasFailure = false;

for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/const"]) {
const passed = normalizedErrors["https://json-schema.org/keyword/const"][schemaLocation] === true;
if (!passed) {
hasFailure = true;
}
const keyword = await getSchema(schemaLocation);
constraints.push({
allowedValues: [Schema.value(keyword)],
schemaLocation
});
}

for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/enum"]) {
const passed = normalizedErrors["https://json-schema.org/keyword/enum"][schemaLocation] === true;
if (!passed) {
hasFailure = true;
}
const keyword = await getSchema(schemaLocation);
constraints.push({
allowedValues: Schema.value(keyword),
schemaLocation
});
}

if (!hasFailure || constraints.length === 0) {
return errors;
}

const sorted = [...constraints].sort((a, b) =>
a.allowedValues.length - b.allowedValues.length
);
const mostConstraining = sorted[0];

let intersection = mostConstraining.allowedValues;
for (let i = 1; i < sorted.length; i++) {
intersection = intersection.filter((/** @type {Json} */ val) =>
sorted[i].allowedValues.some((/** @type {Json} */ other) => jsonEqual(val, other))
);
}
Comment on lines +57 to +67
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of this could be avoided if you used a Set and Set operations to determine the intersection.


const instanceLocation = Instance.uri(instance);
if (intersection.length === 0) {
errors.push({
message: localization.getBooleanSchemaErrorMessage(),
instanceLocation,
schemaLocations: constraints.map((c) => c.schemaLocation)
});
} else if (intersection.length === 1) {
errors.push({
message: localization.getConstErrorMessage(intersection[0]),
instanceLocation,
schemaLocations: [mostConstraining.schemaLocation]
});
} else {
errors.push({
message: localization.getEnumErrorMessage(intersection),
instanceLocation,
schemaLocations: [mostConstraining.schemaLocation]
});
}
return errors;
};

export default constAndEnumErrorHandler;
32 changes: 0 additions & 32 deletions src/error-handlers/enum.js

This file was deleted.

6 changes: 2 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ import unknownNormalizationHandler from "./normalization-handlers/unknown.js";
// Error Handlers
import anyOfErrorHandler from "./error-handlers/anyOf.js";
import booleanSchemaErrorHandler from "./error-handlers/boolean-schema.js";
import constErrorHandler from "./error-handlers/const.js";
import constAndEnumErrorHandler from "./error-handlers/constAndEnum.js";
import containsErrorHandler from "./error-handlers/contains.js";
import dependenciesErrorHandler from "./error-handlers/draft-04/dependencies.js";
import dependentRequiredErrorHandler from "./error-handlers/dependentRequired.js";
import enumErrorHandler from "./error-handlers/enum.js";
import exclusiveMaximumErrorHandler from "./error-handlers/exclusiveMaximum.js";
import exclusiveMinimumErrorHandler from "./error-handlers/exclusiveMinimum.js";
import formatErrorHandler from "./error-handlers/format.js";
Expand Down Expand Up @@ -139,11 +138,10 @@ setNormalizationHandler("https://json-schema.org/keyword/unknown", unknownNormal

addErrorHandler(anyOfErrorHandler);
addErrorHandler(booleanSchemaErrorHandler);
addErrorHandler(constErrorHandler);
addErrorHandler(constAndEnumErrorHandler);
addErrorHandler(containsErrorHandler);
addErrorHandler(dependenciesErrorHandler);
addErrorHandler(dependentRequiredErrorHandler);
addErrorHandler(enumErrorHandler);
addErrorHandler(exclusiveMaximumErrorHandler);
addErrorHandler(exclusiveMinimumErrorHandler);
addErrorHandler(formatErrorHandler);
Expand Down
19 changes: 19 additions & 0 deletions src/test-suite/tests/const.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@
},
"instance": 42,
"errors": []
},
{
"description": "contradictory const - empty intersection",
"compatibility": "6",
"schema": {
"allOf": [
{ "const": "a" },
{ "const": "b" }
]
},
"instance": "a",
"errors": [
{
"messageId": "boolean-schema-message",
"messageParams": {},
"instanceLocation": "#",
"schemaLocations": ["#/allOf/0/const", "#/allOf/1/const"]
}
]
}
]
}
27 changes: 27 additions & 0 deletions src/test-suite/tests/constAndEnum.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"$schema": "../test-suite.schema.json",

"description": "Combined const and enum constraints",
"tests": [
{
"description": "const with enum constraints (combined)",
"compatibility": "6",
"schema": {
"allOf": [
{ "enum": ["a", "b", "c"] },
{ "enum": ["a", "b"] },
{ "const": "a" }
]
},
"instance": "x",
"errors": [
{
"messageId": "const-message",
"messageParams": { "expected": "\"a\"" },
"instanceLocation": "#",
"schemaLocations": ["#/allOf/2/const"]
}
]
}
]
}
40 changes: 40 additions & 0 deletions src/test-suite/tests/enum.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,53 @@
}
]
},
{
"description": "multiple enum constraints (combined)",
"schema": {
"allOf": [
{ "enum": ["a", "b", "c"] },
{ "enum": ["a", "b"] }
]
},
"instance": "x",
"errors": [
{
"messageId": "enum-message",
"messageParams": {
"expected": { "or": ["\"a\"", "\"b\""] },
"count": 2
},
"instanceLocation": "#",
"schemaLocations": ["#/allOf/1/enum"]
}
]
},
{
"description": "enum pass",
"schema": {
"enum": ["foo", "bar"]
},
"instance": "foo",
"errors": []
},
{
"description": "contradictory enum - empty intersection",
"compatibility": "6",
"schema": {
"allOf": [
{ "enum": ["a", "b", "c"] },
{ "enum": ["d", "e", "f"] }
]
},
"instance": "a",
"errors": [
{
"messageId": "boolean-schema-message",
"messageParams": {},
"instanceLocation": "#",
"schemaLocations": ["#/allOf/0/enum", "#/allOf/1/enum"]
}
]
}
]
}