Closed rbuetzer closed 9 months ago
It appears to me that the regex patterns generated by zodToJsonSchema cannot always handle the regex unicode flag.
zodToJsonSchema
Run the follwoing code (Example on StackBlitz)
const zodSchema = z.string().includes("@"); const jsonSchema = zodToJsonSchema(zodSchema); const regex = new RegExp(jsonSchema.pattern, 'u');
invalid identity escape in regular expression
new RegExp(…)
u
No runtime error is thrown
The generated JSON schema looks like this:
{ "type": "string", "pattern": "\\@", "$schema": "http://json-schema.org/draft-07/schema#" }
The problem is, that the @ is escaped.
@
The MDN docs on \ state:
\
Escape sequences like \:, -, \@ will be equivalent to their literal, unescaped character equivalents in regular expressions. However, in regular expressions with the unicode flag, these will cause an invalid identity escape error.
JFC I hate regex
Would adding an option to not escape special characters be a solution?
Try the new patternStrategy option if you run into this.
It appears to me that the regex patterns generated by
zodToJsonSchema
cannot always handle the regex unicode flag.Steps to reproduce
Run the follwoing code (Example on StackBlitz)
Observed behavior
invalid identity escape in regular expression
is thrown in the last linenew RegExp(…)
is called without theu
flag, no error is thrownExpected behavior
No runtime error is thrown
Analysis
The generated JSON schema looks like this:
The problem is, that the
@
is escaped.The MDN docs on
\
state: