From 363a7267f68b1a5738cda2193fa62f8f7bbfe8c3 Mon Sep 17 00:00:00 2001 From: icebob Date: Wed, 1 Apr 2026 20:38:55 +0200 Subject: [PATCH 1/3] fix: escape double-quotes in enum error messages (#293) Enum values containing double-quotes caused a SyntaxError in the compiled validation function. Use JSON.stringify to properly escape the expected value string in the generated code. Co-Authored-By: Claude Opus 4.6 --- lib/rules/enum.js | 2 +- test/rules/enum.spec.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/rules/enum.js b/lib/rules/enum.js index 696399fd..5bd6eedd 100644 --- a/lib/rules/enum.js +++ b/lib/rules/enum.js @@ -7,7 +7,7 @@ module.exports = function({ schema, messages }, path, context) { 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(schema.values.join(", ")), actual: "value", messages })} return value; ` diff --git a/test/rules/enum.spec.js b/test/rules/enum.spec.js index 8459f2ab..07ec6394 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: { From a3f5176bf97df5a068ae0199f1194c282f135c7c Mon Sep 17 00:00:00 2001 From: icebob Date: Wed, 1 Apr 2026 20:39:00 +0200 Subject: [PATCH 2/3] fix: remove | any from ValidationSchema type definition (#334) The | any in the mapped type made the entire type useless since any absorbs all other types in a union. This broke IDE autocompletion and type checking for all TypeScript users. Co-Authored-By: Claude Opus 4.6 --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 3444114b..19fbe6f4 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; } From a904d099bab3a91b0a410dc89ca5318d2ff342dc Mon Sep 17 00:00:00 2001 From: icebob Date: Wed, 1 Apr 2026 20:51:42 +0200 Subject: [PATCH 3/3] fix: use consistent values fallback in enum rule Use the same (schema.values || []) fallback for both the indexOf check and the error message, preventing a potential TypeError if values is missing. Co-Authored-By: Claude Opus 4.6 --- lib/rules/enum.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/rules/enum.js b/lib/rules/enum.js index 5bd6eedd..bd8fb5fe 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: JSON.stringify(schema.values.join(", ")), actual: "value", messages })} + ${this.makeError({ type: "enumValue", expected: JSON.stringify(values.join(", ")), actual: "value", messages })} return value; `