diff --git a/index.d.ts b/index.d.ts index 3444114..19fbe6f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -925,7 +925,7 @@ export type ValidationSchema = ValidationSchemaMetaKeys & { /** * List of validation rules for each defined field */ - [key in keyof T]: ValidationRule | undefined | any; + [key in keyof T]: ValidationRule | undefined; } diff --git a/lib/rules/enum.js b/lib/rules/enum.js index 696399f..bd8fb5f 100644 --- a/lib/rules/enum.js +++ b/lib/rules/enum.js @@ -3,11 +3,12 @@ /** Signature: function(value, field, parent, errors, context) */ module.exports = function({ schema, messages }, path, context) { - const enumStr = JSON.stringify(schema.values || []); + const values = schema.values || []; + const enumStr = JSON.stringify(values); return { source: ` if (${enumStr}.indexOf(value) === -1) - ${this.makeError({ type: "enumValue", expected: "\"" + schema.values.join(", ") + "\"", actual: "value", messages })} + ${this.makeError({ type: "enumValue", expected: JSON.stringify(values.join(", ")), actual: "value", messages })} return value; ` diff --git a/test/rules/enum.spec.js b/test/rules/enum.spec.js index 8459f2a..07ec639 100644 --- a/test/rules/enum.spec.js +++ b/test/rules/enum.spec.js @@ -23,6 +23,19 @@ describe("Test rule: enum", () => { expect(check(false)).toEqual(true); }); + it("should handle enum values containing double-quotes", () => { + const check = v.compile({ $$root: true, type: "enum", values: ["active", "in\"active", "pending"] }); + + expect(check("active")).toEqual(true); + expect(check("pending")).toEqual(true); + expect(check("bad")).toEqual([{ + type: "enumValue", + expected: "active, in\"active, pending", + actual: "bad", + message: "The '' field value 'active, in\"active, pending' does not match any of the allowed values." + }]); + }); + it("should allow custom metas", async () => { const schema = { $$foo: {