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 equal_1 = require("../../runtime/equal");
|
---|
6 | const error = {
|
---|
7 | message: "must be equal to one of the allowed values",
|
---|
8 | params: ({ schemaCode }) => codegen_1._ `{allowedValues: ${schemaCode}}`,
|
---|
9 | };
|
---|
10 | const def = {
|
---|
11 | keyword: "enum",
|
---|
12 | schemaType: "array",
|
---|
13 | $data: true,
|
---|
14 | error,
|
---|
15 | code(cxt) {
|
---|
16 | const { gen, data, $data, schema, schemaCode, it } = cxt;
|
---|
17 | if (!$data && schema.length === 0)
|
---|
18 | throw new Error("enum must have non-empty array");
|
---|
19 | const useLoop = schema.length >= it.opts.loopEnum;
|
---|
20 | const eql = util_1.useFunc(gen, equal_1.default);
|
---|
21 | let valid;
|
---|
22 | if (useLoop || $data) {
|
---|
23 | valid = gen.let("valid");
|
---|
24 | cxt.block$data(valid, loopEnum);
|
---|
25 | }
|
---|
26 | else {
|
---|
27 | /* istanbul ignore if */
|
---|
28 | if (!Array.isArray(schema))
|
---|
29 | throw new Error("ajv implementation error");
|
---|
30 | const vSchema = gen.const("vSchema", schemaCode);
|
---|
31 | valid = codegen_1.or(...schema.map((_x, i) => equalCode(vSchema, i)));
|
---|
32 | }
|
---|
33 | cxt.pass(valid);
|
---|
34 | function loopEnum() {
|
---|
35 | gen.assign(valid, false);
|
---|
36 | gen.forOf("v", schemaCode, (v) => gen.if(codegen_1._ `${eql}(${data}, ${v})`, () => gen.assign(valid, true).break()));
|
---|
37 | }
|
---|
38 | function equalCode(vSchema, i) {
|
---|
39 | const sch = schema[i];
|
---|
40 | return typeof sch === "object" && sch !== null
|
---|
41 | ? codegen_1._ `${eql}(${data}, ${vSchema}[${i}])`
|
---|
42 | : codegen_1._ `${data} === ${sch}`;
|
---|
43 | }
|
---|
44 | },
|
---|
45 | };
|
---|
46 | exports.default = def;
|
---|
47 | //# sourceMappingURL=enum.js.map |
---|