source: trip-planner-front/node_modules/ajv/lib/vocabularies/applicator/oneOf.ts@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.1 KB
Line 
1import type {
2 CodeKeywordDefinition,
3 ErrorObject,
4 KeywordErrorDefinition,
5 AnySchema,
6} from "../../types"
7import type {KeywordCxt} from "../../compile/validate"
8import {_, Name} from "../../compile/codegen"
9import {alwaysValidSchema} from "../../compile/util"
10import {SchemaCxt} from "../../compile"
11
12export type OneOfError = ErrorObject<
13 "oneOf",
14 {passingSchemas: [number, number] | null},
15 AnySchema[]
16>
17
18const error: KeywordErrorDefinition = {
19 message: "must match exactly one schema in oneOf",
20 params: ({params}) => _`{passingSchemas: ${params.passing}}`,
21}
22
23const 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
82export default def
Note: See TracBrowser for help on using the repository browser.