Spaces:
Sleeping
Sleeping
File size: 1,065 Bytes
1df763a |
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 |
import type {CodeKeywordDefinition, KeywordCxt} from "ajv"
import {_, or, and, getProperty, Code} from "ajv/dist/compile/codegen"
export default function getDef(): CodeKeywordDefinition {
return {
keyword: "deepRequired",
type: "object",
schemaType: "array",
code(ctx: KeywordCxt) {
const {schema, data} = ctx
const props = (schema as string[]).map((jp: string) => _`(${getData(jp)}) === undefined`)
ctx.fail(or(...props))
function getData(jsonPointer: string): Code {
if (jsonPointer === "") throw new Error("empty JSON pointer not allowed")
const segments = jsonPointer.split("/")
let x: Code = data
const xs = segments.map((s, i) =>
i ? (x = _`${x}${getProperty(unescapeJPSegment(s))}`) : x
)
return and(...xs)
}
},
metaSchema: {
type: "array",
items: {type: "string", format: "json-pointer"},
},
}
}
function unescapeJPSegment(s: string): string {
return s.replace(/~1/g, "/").replace(/~0/g, "~")
}
module.exports = getDef
|