1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const codegen_1 = require("../../compile/codegen");
|
4 | const metadata_1 = require("./metadata");
|
5 | const nullable_1 = require("./nullable");
|
6 | const error = {
|
7 | message: "must be equal to one of the allowed values",
|
8 | params: ({ schemaCode }) => (0, codegen_1._) `{allowedValues: ${schemaCode}}`,
|
9 | };
|
10 | const def = {
|
11 | keyword: "enum",
|
12 | schemaType: "array",
|
13 | error,
|
14 | code(cxt) {
|
15 | (0, metadata_1.checkMetadata)(cxt);
|
16 | const { gen, data, schema, schemaValue, parentSchema, it } = cxt;
|
17 | if (schema.length === 0)
|
18 | throw new Error("enum must have non-empty array");
|
19 | if (schema.length !== new Set(schema).size)
|
20 | throw new Error("enum items must be unique");
|
21 | let valid;
|
22 | const isString = (0, codegen_1._) `typeof ${data} == "string"`;
|
23 | if (schema.length >= it.opts.loopEnum) {
|
24 | let cond;
|
25 | [valid, cond] = (0, nullable_1.checkNullable)(cxt, isString);
|
26 | gen.if(cond, loopEnum);
|
27 | }
|
28 | else {
|
29 |
|
30 | if (!Array.isArray(schema))
|
31 | throw new Error("ajv implementation error");
|
32 | valid = (0, codegen_1.and)(isString, (0, codegen_1.or)(...schema.map((value) => (0, codegen_1._) `${data} === ${value}`)));
|
33 | if (parentSchema.nullable)
|
34 | valid = (0, codegen_1.or)((0, codegen_1._) `${data} === null`, valid);
|
35 | }
|
36 | cxt.pass(valid);
|
37 | function loopEnum() {
|
38 | gen.forOf("v", schemaValue, (v) => gen.if((0, codegen_1._) `${valid} = ${data} === ${v}`, () => gen.break()));
|
39 | }
|
40 | },
|
41 | };
|
42 | exports.default = def;
|
43 |
|
\ | No newline at end of file |