.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / table / node_modules / ajv / lib / vocabularies / applicator / patternProperties.ts
1 import type {CodeKeywordDefinition} from "../../types"
2 import type {KeywordCxt} from "../../compile/validate"
3 import {schemaProperties, usePattern} from "../code"
4 import {_, not, Name} from "../../compile/codegen"
5 import {checkStrictMode} from "../../compile/util"
6 import {evaluatedPropsToName, Type} from "../../compile/util"
7
8 const def: CodeKeywordDefinition = {
9   keyword: "patternProperties",
10   type: "object",
11   schemaType: "object",
12   code(cxt: KeywordCxt) {
13     const {gen, schema, data, parentSchema, it} = cxt
14     const {opts} = it
15     const patterns = schemaProperties(it, schema)
16     // TODO mark properties matching patterns with always valid schemas as evaluated
17     if (patterns.length === 0) return
18     const checkProperties =
19       opts.strictSchema && !opts.allowMatchingProperties && parentSchema.properties
20     const valid = gen.name("valid")
21     if (it.props !== true && !(it.props instanceof Name)) {
22       it.props = evaluatedPropsToName(gen, it.props)
23     }
24     const {props} = it
25     validatePatternProperties()
26
27     function validatePatternProperties(): void {
28       for (const pat of patterns) {
29         if (checkProperties) checkMatchingProperties(pat)
30         if (it.allErrors) {
31           validateProperties(pat)
32         } else {
33           gen.var(valid, true) // TODO var
34           validateProperties(pat)
35           gen.if(valid)
36         }
37       }
38     }
39
40     function checkMatchingProperties(pat: string): void {
41       for (const prop in checkProperties) {
42         if (new RegExp(pat).test(prop)) {
43           checkStrictMode(
44             it,
45             `property ${prop} matches pattern ${pat} (use allowMatchingProperties)`
46           )
47         }
48       }
49     }
50
51     function validateProperties(pat: string): void {
52       gen.forIn("key", data, (key) => {
53         gen.if(_`${usePattern(gen, pat)}.test(${key})`, () => {
54           cxt.subschema(
55             {
56               keyword: "patternProperties",
57               schemaProp: pat,
58               dataProp: key,
59               dataPropType: Type.Str,
60             },
61             valid
62           )
63           if (it.opts.unevaluated && props !== true) {
64             gen.assign(_`${props}[${key}]`, true)
65           } else if (!it.allErrors) {
66             // can short-circuit if `unevaluatedProperties` is not supported (opts.next === false)
67             // or if all properties were evaluated (props === true)
68             gen.if(not(valid), () => gen.break())
69           }
70         })
71       })
72     }
73   },
74 }
75
76 export default def