.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / table / node_modules / ajv / lib / vocabularies / applicator / oneOf.ts
1 import type {
2   CodeKeywordDefinition,
3   ErrorObject,
4   KeywordErrorDefinition,
5   AnySchema,
6 } from "../../types"
7 import type {KeywordCxt} from "../../compile/validate"
8 import {_, Name} from "../../compile/codegen"
9 import {alwaysValidSchema} from "../../compile/util"
10 import {SchemaCxt} from "../../compile"
11
12 export type OneOfError = ErrorObject<
13   "oneOf",
14   {passingSchemas: [number, number] | null},
15   AnySchema[]
16 >
17
18 const error: KeywordErrorDefinition = {
19   message: "must match exactly one schema in oneOf",
20   params: ({params}) => _`{passingSchemas: ${params.passing}}`,
21 }
22
23 const def: CodeKeywordDefinition = {
24   keyword: "oneOf",
25   schemaType: "array",
26   trackErrors: true,
27   error,
28   code(cxt: KeywordCxt) {
29     const {gen, schema, parentSchema, it} = cxt
30     /* istanbul ignore if */
31     if (!Array.isArray(schema)) throw new Error("ajv implementation error")
32     if (it.opts.discriminator && parentSchema.discriminator) return
33     const schArr: AnySchema[] = schema
34     const valid = gen.let("valid", false)
35     const passing = gen.let("passing", null)
36     const schValid = gen.name("_valid")
37     cxt.setParams({passing})
38     // TODO possibly fail straight away (with warning or exception) if there are two empty always valid schemas
39
40     gen.block(validateOneOf)
41
42     cxt.result(
43       valid,
44       () => cxt.reset(),
45       () => cxt.error(true)
46     )
47
48     function validateOneOf(): void {
49       schArr.forEach((sch: AnySchema, i: number) => {
50         let schCxt: SchemaCxt | undefined
51         if (alwaysValidSchema(it, sch)) {
52           gen.var(schValid, true)
53         } else {
54           schCxt = cxt.subschema(
55             {
56               keyword: "oneOf",
57               schemaProp: i,
58               compositeRule: true,
59             },
60             schValid
61           )
62         }
63
64         if (i > 0) {
65           gen
66             .if(_`${schValid} && ${valid}`)
67             .assign(valid, false)
68             .assign(passing, _`[${passing}, ${i}]`)
69             .else()
70         }
71
72         gen.if(schValid, () => {
73           gen.assign(valid, true)
74           gen.assign(passing, i)
75           if (schCxt) cxt.mergeEvaluated(schCxt, Name)
76         })
77       })
78     }
79   },
80 }
81
82 export default def