.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / table / node_modules / ajv / lib / vocabularies / validation / required.ts
1 import type {CodeKeywordDefinition, ErrorObject, KeywordErrorDefinition} from "../../types"
2 import type {KeywordCxt} from "../../compile/validate"
3 import {
4   checkReportMissingProp,
5   checkMissingProp,
6   reportMissingProp,
7   propertyInData,
8   noPropertyInData,
9 } from "../code"
10 import {_, str, nil, not, Name, Code} from "../../compile/codegen"
11 import {checkStrictMode} from "../../compile/util"
12
13 export type RequiredError = ErrorObject<
14   "required",
15   {missingProperty: string},
16   string[] | {$data: string}
17 >
18
19 const error: KeywordErrorDefinition = {
20   message: ({params: {missingProperty}}) => str`must have required property '${missingProperty}'`,
21   params: ({params: {missingProperty}}) => _`{missingProperty: ${missingProperty}}`,
22 }
23
24 const def: CodeKeywordDefinition = {
25   keyword: "required",
26   type: "object",
27   schemaType: "array",
28   $data: true,
29   error,
30   code(cxt: KeywordCxt) {
31     const {gen, schema, schemaCode, data, $data, it} = cxt
32     const {opts} = it
33     if (!$data && schema.length === 0) return
34     const useLoop = schema.length >= opts.loopRequired
35     if (it.allErrors) allErrorsMode()
36     else exitOnErrorMode()
37
38     if (opts.strictRequired) {
39       const props = cxt.parentSchema.properties
40       const {definedProperties} = cxt.it
41       for (const requiredKey of schema) {
42         if (props?.[requiredKey] === undefined && !definedProperties.has(requiredKey)) {
43           const schemaPath = it.schemaEnv.baseId + it.errSchemaPath
44           const msg = `required property "${requiredKey}" is not defined at "${schemaPath}" (strictRequired)`
45           checkStrictMode(it, msg, it.opts.strictRequired)
46         }
47       }
48     }
49
50     function allErrorsMode(): void {
51       if (useLoop || $data) {
52         cxt.block$data(nil, loopAllRequired)
53       } else {
54         for (const prop of schema) {
55           checkReportMissingProp(cxt, prop)
56         }
57       }
58     }
59
60     function exitOnErrorMode(): void {
61       const missing = gen.let("missing")
62       if (useLoop || $data) {
63         const valid = gen.let("valid", true)
64         cxt.block$data(valid, () => loopUntilMissing(missing, valid))
65         cxt.ok(valid)
66       } else {
67         gen.if(checkMissingProp(cxt, schema, missing))
68         reportMissingProp(cxt, missing)
69         gen.else()
70       }
71     }
72
73     function loopAllRequired(): void {
74       gen.forOf("prop", schemaCode as Code, (prop) => {
75         cxt.setParams({missingProperty: prop})
76         gen.if(noPropertyInData(gen, data, prop, opts.ownProperties), () => cxt.error())
77       })
78     }
79
80     function loopUntilMissing(missing: Name, valid: Name): void {
81       cxt.setParams({missingProperty: missing})
82       gen.forOf(
83         missing,
84         schemaCode as Code,
85         () => {
86           gen.assign(valid, propertyInData(gen, data, missing, opts.ownProperties))
87           gen.if(not(valid), () => {
88             cxt.error()
89             gen.break()
90           })
91         },
92         nil
93       )
94     }
95   },
96 }
97
98 export default def