main
Last change
on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago |
F4 Finalna Verzija
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Line | |
---|
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
|
---|
Note:
See
TracBrowser
for help on using the repository browser.