Skip to content

Commit 3e8a342

Browse files
committed
handle types array
1 parent a2b7060 commit 3e8a342

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

src/error-handlers/anyOf.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const anyOfErrorHandler = async (normalizedErrors, instance, localization) => {
4545
// No alternative matched the type/enum/const of the instance.
4646
if (alternatives.length === 0) {
4747
/** @type Set<string> */
48-
const expectedTypes = new Set();
48+
let expectedTypes = new Set();
4949

5050
/** @type Set<Json> */
5151
const expectedEnums = new Set();
@@ -55,13 +55,19 @@ const anyOfErrorHandler = async (normalizedErrors, instance, localization) => {
5555
if (instanceLocation === Instance.uri(instance)) {
5656
let alternativeTypes = new Set(["null", "boolean", "number", "string", "array", "object"]);
5757
for (const schemaLocation in alternative[instanceLocation]["https://json-schema.org/keyword/type"]) {
58-
// TODO: Support type arrays
5958
const keyword = await getSchema(schemaLocation);
60-
const expectedType = /** @type string */ (Schema.value(keyword));
61-
alternativeTypes = alternativeTypes.intersection(new Set(([expectedType])));
59+
if (Schema.typeOf(keyword) === "array") {
60+
const expectedTypes = /** @type string[] */ (Schema.value(keyword));
61+
alternativeTypes = alternativeTypes.intersection(new Set(expectedTypes));
62+
} else {
63+
const expectedType = /** @type string */ (Schema.value(keyword));
64+
alternativeTypes = alternativeTypes.intersection(new Set([expectedType]));
65+
}
6266
}
63-
if (alternativeTypes.size === 1) {
64-
expectedTypes.add([...alternativeTypes][0]);
67+
68+
// The are 6 types. If all types are allowed, don't use expectedTypes
69+
if (alternativeTypes.size !== 6) {
70+
expectedTypes = expectedTypes.union(alternativeTypes);
6571
}
6672

6773
for (const schemaLocation in alternative[instanceLocation]["https://json-schema.org/keyword/enum"]) {
@@ -79,7 +85,6 @@ const anyOfErrorHandler = async (normalizedErrors, instance, localization) => {
7985
}
8086
}
8187
}
82-
8388
errors.push({
8489
message: localization.getEnumErrorMessage({
8590
allowedValues: [...expectedEnums],

src/keyword-error-message.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,47 @@ describe("Error messages", async () => {
13291329
]);
13301330
});
13311331

1332+
test("anyOf with type arrays", async () => {
1333+
registerSchema({
1334+
$schema: "https://json-schema.org/draft/2020-12/schema",
1335+
anyOf: [
1336+
{ type: ["string", "number"] },
1337+
{ type: "boolean" }
1338+
]
1339+
}, schemaUri);
1340+
1341+
const instance = null;
1342+
1343+
/** @type OutputFormat */
1344+
const output = {
1345+
valid: false,
1346+
errors: [
1347+
{
1348+
absoluteKeywordLocation: "https://example.com/main#/anyOf/0/type",
1349+
instanceLocation: "#"
1350+
},
1351+
{
1352+
absoluteKeywordLocation: "https://example.com/main#/anyOf/1/type",
1353+
instanceLocation: "#"
1354+
},
1355+
{
1356+
absoluteKeywordLocation: "https://example.com/main#/anyOf",
1357+
instanceLocation: "#"
1358+
}
1359+
]
1360+
};
1361+
1362+
const result = await betterJsonSchemaErrors(output, schemaUri, instance);
1363+
1364+
expect(result.errors).to.eql([
1365+
{
1366+
schemaLocation: "https://example.com/main#/anyOf",
1367+
instanceLocation: "#",
1368+
message: localization.getEnumErrorMessage({ allowedTypes: ["string", "number", "boolean"] }, null)
1369+
}
1370+
]);
1371+
});
1372+
13321373
test("anyOf with string alternatives", async () => {
13331374
registerSchema({
13341375
$schema: "https://json-schema.org/draft/2020-12/schema",

0 commit comments

Comments
 (0)