1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | const codegen_1 = require("ajv/dist/compile/codegen");
|
---|
4 | const _util_1 = require("./_util");
|
---|
5 | const error = {
|
---|
6 | message: ({ params: { schemaProp } }) => schemaProp
|
---|
7 | ? (0, codegen_1.str) `should match case "${schemaProp}" schema`
|
---|
8 | : (0, codegen_1.str) `should match default case schema`,
|
---|
9 | params: ({ params: { schemaProp } }) => schemaProp ? (0, codegen_1._) `{failingCase: ${schemaProp}}` : (0, codegen_1._) `{failingDefault: true}`,
|
---|
10 | };
|
---|
11 | function getDef(opts) {
|
---|
12 | const metaSchema = (0, _util_1.metaSchemaRef)(opts);
|
---|
13 | return [
|
---|
14 | {
|
---|
15 | keyword: "select",
|
---|
16 | schemaType: ["string", "number", "boolean", "null"],
|
---|
17 | $data: true,
|
---|
18 | error,
|
---|
19 | dependencies: ["selectCases"],
|
---|
20 | code(cxt) {
|
---|
21 | const { gen, schemaCode, parentSchema } = cxt;
|
---|
22 | cxt.block$data(codegen_1.nil, () => {
|
---|
23 | const valid = gen.let("valid", true);
|
---|
24 | const schValid = gen.name("_valid");
|
---|
25 | const value = gen.const("value", (0, codegen_1._) `${schemaCode} === null ? "null" : ${schemaCode}`);
|
---|
26 | gen.if(false); // optimizer should remove it from generated code
|
---|
27 | for (const schemaProp in parentSchema.selectCases) {
|
---|
28 | cxt.setParams({ schemaProp });
|
---|
29 | gen.elseIf((0, codegen_1._) `"" + ${value} == ${schemaProp}`); // intentional ==, to match numbers and booleans
|
---|
30 | const schCxt = cxt.subschema({ keyword: "selectCases", schemaProp }, schValid);
|
---|
31 | cxt.mergeEvaluated(schCxt, codegen_1.Name);
|
---|
32 | gen.assign(valid, schValid);
|
---|
33 | }
|
---|
34 | gen.else();
|
---|
35 | if (parentSchema.selectDefault !== undefined) {
|
---|
36 | cxt.setParams({ schemaProp: undefined });
|
---|
37 | const schCxt = cxt.subschema({ keyword: "selectDefault" }, schValid);
|
---|
38 | cxt.mergeEvaluated(schCxt, codegen_1.Name);
|
---|
39 | gen.assign(valid, schValid);
|
---|
40 | }
|
---|
41 | gen.endIf();
|
---|
42 | cxt.pass(valid);
|
---|
43 | });
|
---|
44 | },
|
---|
45 | },
|
---|
46 | {
|
---|
47 | keyword: "selectCases",
|
---|
48 | dependencies: ["select"],
|
---|
49 | metaSchema: {
|
---|
50 | type: "object",
|
---|
51 | additionalProperties: metaSchema,
|
---|
52 | },
|
---|
53 | },
|
---|
54 | {
|
---|
55 | keyword: "selectDefault",
|
---|
56 | dependencies: ["select", "selectCases"],
|
---|
57 | metaSchema,
|
---|
58 | },
|
---|
59 | ];
|
---|
60 | }
|
---|
61 | exports.default = getDef;
|
---|
62 | module.exports = getDef;
|
---|
63 | //# sourceMappingURL=select.js.map |
---|