Fix enum double-quotes crash and TS ValidationSchema type#361
Conversation
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Fixes two reported issues in the validator: generated enum validation code now safely handles enum values containing double-quotes, and the ValidationSchema TypeScript type no longer collapses to any.
Changes:
- Escape enum expected-value strings in generated code via
JSON.stringify()to prevent compile-timeSyntaxErrors. - Remove
| anyfrom theValidationSchemamapped type to restore useful static typing. - Add a regression test covering enum values containing
".
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
lib/rules/enum.js |
Updates generated enum error metadata to use a safely-escaped expected string. |
index.d.ts |
Fixes ValidationSchema typing by removing ` |
test/rules/enum.spec.js |
Adds a regression test ensuring enums with embedded " compile and validate correctly. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
lib/rules/enum.js
Outdated
| @@ -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 })} | |||
There was a problem hiding this comment.
schema.values is defaulted to [] for enumStr, but expected still calls schema.values.join(...). If values is missing or not an array, this will throw during compilation (TypeError) even though the rule otherwise tries to handle an empty default. Consider using the same fallback for the expected string (or validating values up-front and throwing a clear schema error).
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 <noreply@anthropic.com>
Summary
Two bug fixes:
") caused aSyntaxErrorat compile time because quotes weren't escaped in the generated validation code. Fixed by usingJSON.stringify()to properly escape the expected value string.ValidationSchemaTypeScript type had| anyin the mapped type union, which made the entire type useless (any absorbs all other types). Removed| any, keepingValidationRule | undefined.Changes
lib/rules/enum.js— UseJSON.stringify()instead of manual quote wrappingindex.d.ts— Remove| anyfromValidationSchemamapped typetest/rules/enum.spec.js— Added test for enum values with double-quotesTest plan
🤖 Generated with Claude Code