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");
7 message: "must be equal to one of the allowed values",
8 params: ({ schemaCode }) => codegen_1._ `{allowedValues: ${schemaCode}}`,
15 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");
22 const isString = codegen_1._ `typeof ${data} == "string"`;
23 if (schema.length >= it.opts.loopEnum) {
25 [valid, cond] = nullable_1.checkNullable(cxt, isString);
26 gen.if(cond, loopEnum);
29 /* istanbul ignore if */
30 if (!Array.isArray(schema))
31 throw new Error("ajv implementation error");
32 valid = codegen_1.and(isString, codegen_1.or(...schema.map((value) => codegen_1._ `${data} === ${value}`)));
33 if (parentSchema.nullable)
34 valid = codegen_1.or(codegen_1._ `${data} === null`, valid);
38 gen.forOf("v", schemaValue, (v) => gen.if(codegen_1._ `${valid} = ${data} === ${v}`, () => gen.break()));
42 exports.default = def;
43 //# sourceMappingURL=enum.js.map