Skip to content

Commit 9755206

Browse files
fixed type error handler to handle allOf
Signed-off-by: Diya <[email protected]>
1 parent 9380015 commit 9755206

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/error-handlers/type.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,34 @@ const type = async (normalizedErrors, instance, localization) => {
1212
const errors = [];
1313

1414
if (normalizedErrors["https://json-schema.org/keyword/type"]) {
15+
let allowedTypes = new Set(["null", "boolean", "number", "string", "array", "object", "integer"]);
16+
const failedTypeLocations = [];
17+
1518
for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/type"]) {
16-
if (!normalizedErrors["https://json-schema.org/keyword/type"][schemaLocation]) {
19+
const isValid = normalizedErrors["https://json-schema.org/keyword/type"][schemaLocation];
20+
if (!isValid) {
21+
failedTypeLocations.push(schemaLocation);
22+
}
23+
24+
const keyword = await getSchema(schemaLocation);
25+
/** @type {string|string[]} */
26+
const value = Schema.value(keyword);
27+
const types = Array.isArray(value) ? value : [value];
28+
/** @type {Set<string>} */
29+
const keywordTypes = new Set(types);
30+
allowedTypes = allowedTypes.intersection(keywordTypes);
31+
}
32+
33+
if (allowedTypes.size === 0) {
34+
if (failedTypeLocations.length > 0) {
35+
errors.push({
36+
message: localization.getConflictingTypeMessage(),
37+
instanceLocation: Instance.uri(instance),
38+
schemaLocation: failedTypeLocations[0]
39+
});
40+
}
41+
} else {
42+
for (const schemaLocation of failedTypeLocations) {
1743
const keyword = await getSchema(schemaLocation);
1844
errors.push({
1945
message: localization.getTypeErrorMessage(Schema.value(keyword), Instance.typeOf(instance)),

src/keyword-error-message.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1719,7 +1719,7 @@ describe("Error messages", async () => {
17191719
]);
17201720
});
17211721

1722-
test.skip("allOf conflicting type", async () => {
1722+
test("allOf conflicting type", async () => {
17231723
registerSchema({
17241724
$schema: "https://json-schema.org/draft/2020-12/schema",
17251725
allOf: [

0 commit comments

Comments
 (0)