-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathformat.ts
More file actions
41 lines (37 loc) · 1.29 KB
/
format.ts
File metadata and controls
41 lines (37 loc) · 1.29 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
import { Keyword, JsonSchemaValidatorParams } from "../Keyword";
import { SchemaNode } from "../SchemaNode";
const KEYWORD = "format";
export const formatKeyword: Keyword = {
id: KEYWORD,
keyword: KEYWORD,
parse: parseFormat,
addValidate: ({ schema }) => schema?.format != null,
validate: validateFormat
};
function parseFormat(node: SchemaNode) {
const format = node.schema[KEYWORD];
if (format == null) {
return;
}
if (typeof format !== "string") {
return node.createError("schema-error", {
pointer: `${node.schemaLocation}/${KEYWORD}`,
schema: node.schema,
value: format,
message: `Keyword '${KEYWORD}' must be a string - received '${typeof format}'`
});
}
if (node.context.formats[format] == null) {
return node.createAnnotation("unknown-format-warning", {
pointer: `${node.schemaLocation}/${KEYWORD}`,
schema: node.schema,
value: format,
message: `Keyword '${KEYWORD}' must be a string - received '${typeof format}'`
});
}
}
function validateFormat(options: JsonSchemaValidatorParams) {
const { node } = options;
const formatValidator = node.context.formats[node.schema.format];
return formatValidator?.(options);
}