4 KeywordErrorDefinition,
7 import type {KeywordCxt} from "../../compile/validate"
8 import {_, Name} from "../../compile/codegen"
9 import {alwaysValidSchema} from "../../compile/util"
10 import {SchemaCxt} from "../../compile"
12 export type OneOfError = ErrorObject<
14 {passingSchemas: [number, number] | null},
18 const error: KeywordErrorDefinition = {
19 message: "must match exactly one schema in oneOf",
20 params: ({params}) => _`{passingSchemas: ${params.passing}}`,
23 const def: CodeKeywordDefinition = {
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
40 gen.block(validateOneOf)
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)
54 schCxt = cxt.subschema(
66 .if(_`${schValid} && ${valid}`)
68 .assign(passing, _`[${passing}, ${i}]`)
72 gen.if(schValid, () => {
73 gen.assign(valid, true)
74 gen.assign(passing, i)
75 if (schCxt) cxt.mergeEvaluated(schCxt, Name)