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