generated from json-schema-org/repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathvalidators.ts
78 lines (74 loc) · 2.08 KB
/
validators.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import {
validate,
registerSchema,
setMetaSchemaOutputFormat,
unregisterSchema,
SchemaObject,
OutputUnit,
} from "@hyperjump/json-schema/draft-2020-12";
import { BASIC } from "@hyperjump/json-schema/experimental";
import { hasNestedProperty } from "./client-functions";
setMetaSchemaOutputFormat(BASIC);
export const schemaUrl = "http://tour.json-schema.org/";
export async function hyperjumpValidate(
data: any,
schema: any,
externalSchema?: any,
) {
if (!("$schema" in schema)) {
schema["$schema"] = "https://json-schema.org/draft/2020-12/schema";
}
try {
registerSchema(schema as SchemaObject, schemaUrl);
if (externalSchema) {
registerSchema(externalSchema as SchemaObject, externalSchema.$id);
}
const output = await validate(schemaUrl, data as SchemaObject, BASIC);
return output;
} catch (e) {
throw e;
} finally {
unregisterSchema(schemaUrl);
if (externalSchema) {
unregisterSchema(externalSchema.$id);
}
}
}
export async function hyperjumpCheckAnnotations(
schema: any,
requiredAnnotations: string[],
): Promise<OutputUnit> {
// const annotationSchemaUrl = "http://tour2.json-schemad.org/";
const dialectId = "https://json-schema.org/draft/2020-12/schema";
if (!("$schema" in schema)) {
schema["$schema"] = dialectId;
}
try {
// registerSchema(schema as SchemaObject, annotationSchemaUrl);
// const instance = await annotate(annotationSchemaUrl, data as SchemaObject);
const missingAnnotations: string[] = [];
for (const annotation of requiredAnnotations) {
if (!hasNestedProperty(schema, annotation)) {
missingAnnotations.push(annotation);
}
}
if (missingAnnotations.length > 0) {
throw new Error(
`Schema does not contain the following annotations: ${missingAnnotations.join(
", ",
)}`,
);
}
return {
valid: true,
keyword: "",
instanceLocation: "",
absoluteKeywordLocation: "",
};
} catch (e) {
console.log(e);
throw e;
} finally {
// unregisterSchema(annotationSchemaUrl);
}
}