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

This file was deleted.

78 changes: 78 additions & 0 deletions src/error-handlers/constEnum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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, Json } from "../index.d.ts"
*/

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

/** @type {ErrorHandler} */
const constEnumErrorHandler = async (normalizedErrors, instance, localization) => {
/** @type Set<string> | undefined */
let allowedJson;

/** @type string[]> */
const constSchemaLocations = [];

/** @type string[]> */
const enumSchemaLocations = [];

/** @type string[]> */
const allSchemaLocations = [];

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

const keyword = await getSchema(schemaLocation);
const keywordJson = new Set([jsonStringify(/** @type Json */ (Schema.value(keyword)))]);

allowedJson = allowedJson?.intersection(keywordJson) ?? keywordJson;
}

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

const keyword = await getSchema(schemaLocation);
const keywordJson = new Set(/** @type Json[] */ (Schema.value(keyword)).map((value) => jsonStringify(value)));

allowedJson = allowedJson?.intersection(keywordJson) ?? keywordJson;
}

if (constSchemaLocations.length === 0 && enumSchemaLocations.length === 0) {
return [];
}

if (allowedJson?.size === 0) {
return [{
message: localization.getBooleanSchemaErrorMessage(),
instanceLocation: Instance.uri(instance),
schemaLocations: allSchemaLocations
}];
} else {
/** @type Json[] */
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
const allowedValues = [...allowedJson ?? []].map((json) => JSON.parse(json));

return [{
message: localization.getEnumErrorMessage(allowedValues),
instanceLocation: Instance.uri(instance),
schemaLocations: constSchemaLocations.length ? constSchemaLocations : enumSchemaLocations
}];
}
};

export default constEnumErrorHandler;
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 @@ -55,10 +55,9 @@ 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 constEnumErrorHandler from "./error-handlers/constEnum.js";
import containsErrorHandler from "./error-handlers/contains.js";
import dependenciesErrorHandler from "./error-handlers/draft-04/dependencies.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 @@ -140,10 +139,9 @@ setNormalizationHandler("https://json-schema.org/keyword/unknown", unknownNormal

addErrorHandler(anyOfErrorHandler);
addErrorHandler(booleanSchemaErrorHandler);
addErrorHandler(constErrorHandler);
addErrorHandler(constEnumErrorHandler);
addErrorHandler(containsErrorHandler);
addErrorHandler(dependenciesErrorHandler);
addErrorHandler(enumErrorHandler);
addErrorHandler(exclusiveMaximumErrorHandler);
addErrorHandler(exclusiveMinimumErrorHandler);
addErrorHandler(formatErrorHandler);
Expand Down
21 changes: 10 additions & 11 deletions src/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,18 @@ export class Localization {
});
}

/** @type (expected: Json) => string */
getConstErrorMessage(expected) {
return this.#formatMessage("const-message", {
expected: JSON.stringify(expected, null, " ")
});
}

/** @type (expected: Json[]) => string */
getEnumErrorMessage(expected) {
const expectedJson = expected.map((value) => JSON.stringify(value));
return this.#formatMessage("enum-message", {
expected: this.disjunction.format(expectedJson)
});
if (expected.length === 1) {
return this.#formatMessage("const-message", {
expected: JSON.stringify(expected[0], null, " ")
});
} else {
const expectedJson = expected.map((value) => JSON.stringify(value));
return this.#formatMessage("enum-message", {
expected: this.disjunction.format(expectedJson)
});
}
}

/** @type (format: string) => string */
Expand Down
63 changes: 63 additions & 0 deletions src/test-suite/tests/const.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,69 @@
},
"instance": 42,
"errors": []
},
{
"description": "const with enum",
"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"]
}
]
},
{
"description": "const with enum - deterministic key order",
"compatibility": "6",
"schema": {
"allOf": [
{ "enum": [{ "a": 1, "b": 2 }, { "c": 3 }] },
{ "const": { "b": 2, "a": 1 } }
]
},
"instance": "x",
"errors": [
{
"messageId": "const-message",
"messageParams": {
"expected": "{\n \"a\": 1,\n \"b\": 2\n}"
},
"instanceLocation": "#",
"schemaLocations": ["#/allOf/1/const"]
}
]
},
{
"description": "contradictory const",
"compatibility": "6",
"schema": {
"allOf": [
{ "const": "a" },
{ "const": "b" }
]
},
"instance": "a",
"errors": [
{
"messageId": "boolean-schema-message",
"messageParams": {},
"instanceLocation": "#",
"schemaLocations": [
"#/allOf/0/const",
"#/allOf/1/const"
]
}
]
}
]
}
71 changes: 71 additions & 0 deletions src/test-suite/tests/enum.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,77 @@
},
"instance": "foo",
"errors": []
},
{
"description": "multiple enums with matches",
"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/0/enum",
"#/allOf/1/enum"
]
}
]
},
{
"description": "multiple enums with a match",
"compatibility": "6",
"schema": {
"allOf": [
{ "enum": ["a", "b", "c"] },
{ "enum": ["a", "b", "d"] },
{ "enum": ["a", "b", "e"] }
]
},
"instance": "c",
"errors": [
{
"messageId": "enum-message",
"messageParams": {
"expected": { "or": ["\"a\"", "\"b\""] }
},
"instanceLocation": "#",
"schemaLocations": [
"#/allOf/1/enum",
"#/allOf/2/enum"
]
}
]
},
{
"description": "contradictory enum",
"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"
]
}
]
}
]
}