1 | import type {CodeKeywordDefinition, KeywordCxt} from "ajv"
|
2 | import {_, or, and, getProperty, Code} from "ajv/dist/compile/codegen"
|
3 |
|
4 | export default function getDef(): CodeKeywordDefinition {
|
5 | return {
|
6 | keyword: "deepRequired",
|
7 | type: "object",
|
8 | schemaType: "array",
|
9 | code(ctx: KeywordCxt) {
|
10 | const {schema, data} = ctx
|
11 | const props = (schema as string[]).map((jp: string) => _`(${getData(jp)}) === undefined`)
|
12 | ctx.fail(or(...props))
|
13 |
|
14 | function getData(jsonPointer: string): Code {
|
15 | if (jsonPointer === "") throw new Error("empty JSON pointer not allowed")
|
16 | const segments = jsonPointer.split("/")
|
17 | let x: Code = data
|
18 | const xs = segments.map((s, i) =>
|
19 | i ? (x = _`${x}${getProperty(unescapeJPSegment(s))}`) : x
|
20 | )
|
21 | return and(...xs)
|
22 | }
|
23 | },
|
24 | metaSchema: {
|
25 | type: "array",
|
26 | items: {type: "string", format: "json-pointer"},
|
27 | },
|
28 | }
|
29 | }
|
30 |
|
31 | function unescapeJPSegment(s: string): string {
|
32 | return s.replace(/~1/g, "/").replace(/~0/g, "~")
|
33 | }
|
34 |
|
35 | module.exports = getDef
|
36 |
|
\ | No newline at end of file |