-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathenum.ts
29 lines (27 loc) · 885 Bytes
/
enum.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { Keyword, JsonSchemaValidatorParams } from "../Keyword";
import { getTypeOf } from "../utils/getTypeOf";
export const enumKeyword: Keyword = {
id: "enum",
keyword: "enum",
addValidate: ({ schema }) => Array.isArray(schema.enum),
validate: validateEnum
};
function validateEnum({ node, data, pointer = "#" }: JsonSchemaValidatorParams) {
const { schema } = node;
const type = getTypeOf(data);
if (type === "object" || type === "array") {
const valueStr = JSON.stringify(data);
for (let i = 0; i < schema.enum.length; i += 1) {
if (JSON.stringify(schema.enum[i]) === valueStr) {
return undefined;
}
}
} else if (schema.enum.includes(data)) {
return undefined;
}
return node.createError("enum-error", {
pointer,
schema,
value: data
});
}