Skip to content

Fix enum double-quotes crash and TS ValidationSchema type#361

Merged
icebob merged 3 commits intomasterfrom
feature/fix-293-334-309
Apr 1, 2026
Merged

Fix enum double-quotes crash and TS ValidationSchema type#361
icebob merged 3 commits intomasterfrom
feature/fix-293-334-309

Conversation

@icebob-ai
Copy link
Copy Markdown
Collaborator

Summary

Two bug fixes:

Changes

  • lib/rules/enum.js — Use JSON.stringify() instead of manual quote wrapping
  • index.d.ts — Remove | any from ValidationSchema mapped type
  • test/rules/enum.spec.js — Added test for enum values with double-quotes

Test plan

  • 361/361 tests pass
  • Enum with double-quotes compiles and validates correctly
  • Existing enum tests unchanged and passing

🤖 Generated with Claude Code

icebob1 and others added 2 commits April 1, 2026 20:38
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>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-time SyntaxErrors.
  • Remove | any from the ValidationSchema mapped 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.

Comment on lines +6 to +10
@@ -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 })}
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
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>
@icebob icebob merged commit 6a3bc06 into master Apr 1, 2026
8 checks passed
@icebob icebob deleted the feature/fix-293-334-309 branch April 1, 2026 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants