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: ({ params }) => codegen_1.str `must match "${params.ifClause}" schema`,
|
---|
7 | params: ({ params }) => codegen_1._ `{failingKeyword: ${params.ifClause}}`,
|
---|
8 | };
|
---|
9 | const def = {
|
---|
10 | keyword: "if",
|
---|
11 | schemaType: ["object", "boolean"],
|
---|
12 | trackErrors: true,
|
---|
13 | error,
|
---|
14 | code(cxt) {
|
---|
15 | const { gen, parentSchema, it } = cxt;
|
---|
16 | if (parentSchema.then === undefined && parentSchema.else === undefined) {
|
---|
17 | util_1.checkStrictMode(it, '"if" without "then" and "else" is ignored');
|
---|
18 | }
|
---|
19 | const hasThen = hasSchema(it, "then");
|
---|
20 | const hasElse = hasSchema(it, "else");
|
---|
21 | if (!hasThen && !hasElse)
|
---|
22 | return;
|
---|
23 | const valid = gen.let("valid", true);
|
---|
24 | const schValid = gen.name("_valid");
|
---|
25 | validateIf();
|
---|
26 | cxt.reset();
|
---|
27 | if (hasThen && hasElse) {
|
---|
28 | const ifClause = gen.let("ifClause");
|
---|
29 | cxt.setParams({ ifClause });
|
---|
30 | gen.if(schValid, validateClause("then", ifClause), validateClause("else", ifClause));
|
---|
31 | }
|
---|
32 | else if (hasThen) {
|
---|
33 | gen.if(schValid, validateClause("then"));
|
---|
34 | }
|
---|
35 | else {
|
---|
36 | gen.if(codegen_1.not(schValid), validateClause("else"));
|
---|
37 | }
|
---|
38 | cxt.pass(valid, () => cxt.error(true));
|
---|
39 | function validateIf() {
|
---|
40 | const schCxt = cxt.subschema({
|
---|
41 | keyword: "if",
|
---|
42 | compositeRule: true,
|
---|
43 | createErrors: false,
|
---|
44 | allErrors: false,
|
---|
45 | }, schValid);
|
---|
46 | cxt.mergeEvaluated(schCxt);
|
---|
47 | }
|
---|
48 | function validateClause(keyword, ifClause) {
|
---|
49 | return () => {
|
---|
50 | const schCxt = cxt.subschema({ keyword }, schValid);
|
---|
51 | gen.assign(valid, schValid);
|
---|
52 | cxt.mergeValidEvaluated(schCxt, valid);
|
---|
53 | if (ifClause)
|
---|
54 | gen.assign(ifClause, codegen_1._ `${keyword}`);
|
---|
55 | else
|
---|
56 | cxt.setParams({ ifClause: keyword });
|
---|
57 | };
|
---|
58 | }
|
---|
59 | },
|
---|
60 | };
|
---|
61 | function hasSchema(it, keyword) {
|
---|
62 | const schema = it.schema[keyword];
|
---|
63 | return schema !== undefined && !util_1.alwaysValidSchema(it, schema);
|
---|
64 | }
|
---|
65 | exports.default = def;
|
---|
66 | //# sourceMappingURL=if.js.map |
---|