1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | const codegen_1 = require("../../compile/codegen");
|
---|
4 | const util_1 = require("../../compile/util");
|
---|
5 | const error = {
|
---|
6 | message: "must match exactly one schema in oneOf",
|
---|
7 | params: ({ params }) => codegen_1._ `{passingSchemas: ${params.passing}}`,
|
---|
8 | };
|
---|
9 | const def = {
|
---|
10 | keyword: "oneOf",
|
---|
11 | schemaType: "array",
|
---|
12 | trackErrors: true,
|
---|
13 | error,
|
---|
14 | code(cxt) {
|
---|
15 | const { gen, schema, parentSchema, it } = cxt;
|
---|
16 | /* istanbul ignore if */
|
---|
17 | if (!Array.isArray(schema))
|
---|
18 | throw new Error("ajv implementation error");
|
---|
19 | if (it.opts.discriminator && parentSchema.discriminator)
|
---|
20 | return;
|
---|
21 | const schArr = schema;
|
---|
22 | const valid = gen.let("valid", false);
|
---|
23 | const passing = gen.let("passing", null);
|
---|
24 | const schValid = gen.name("_valid");
|
---|
25 | cxt.setParams({ passing });
|
---|
26 | // TODO possibly fail straight away (with warning or exception) if there are two empty always valid schemas
|
---|
27 | gen.block(validateOneOf);
|
---|
28 | cxt.result(valid, () => cxt.reset(), () => cxt.error(true));
|
---|
29 | function validateOneOf() {
|
---|
30 | schArr.forEach((sch, i) => {
|
---|
31 | let schCxt;
|
---|
32 | if (util_1.alwaysValidSchema(it, sch)) {
|
---|
33 | gen.var(schValid, true);
|
---|
34 | }
|
---|
35 | else {
|
---|
36 | schCxt = cxt.subschema({
|
---|
37 | keyword: "oneOf",
|
---|
38 | schemaProp: i,
|
---|
39 | compositeRule: true,
|
---|
40 | }, schValid);
|
---|
41 | }
|
---|
42 | if (i > 0) {
|
---|
43 | gen
|
---|
44 | .if(codegen_1._ `${schValid} && ${valid}`)
|
---|
45 | .assign(valid, false)
|
---|
46 | .assign(passing, codegen_1._ `[${passing}, ${i}]`)
|
---|
47 | .else();
|
---|
48 | }
|
---|
49 | gen.if(schValid, () => {
|
---|
50 | gen.assign(valid, true);
|
---|
51 | gen.assign(passing, i);
|
---|
52 | if (schCxt)
|
---|
53 | cxt.mergeEvaluated(schCxt, codegen_1.Name);
|
---|
54 | });
|
---|
55 | });
|
---|
56 | }
|
---|
57 | },
|
---|
58 | };
|
---|
59 | exports.default = def;
|
---|
60 | //# sourceMappingURL=oneOf.js.map |
---|