Closed Spappz closed 4 months ago
Don't have time for a thorough review right now, but this PR just makes me all warm and fuzzy. Issue->Discussion->PR with a great description, tests included, and readme updated. Bravo!
Sorry the wait! Great work.
Thank you! Glad to have helped.
Though, as a nit, I think you mistook me for another guy with an open PR in 23195b3 and 9275d61 😅
@Spappz I'm an idiot. Fixed though :) https://github.com/StefanTerdell/zod-to-json-schema/commit/3e7c648e626a73517bccb846869d15ef66531a00
Summary
Reworked the string parser to attempt to mutate provided regexes into a flag-independent form. When the
applyRegexFlags
option is falsy, nothing actually changes; theregex.source
that was previously fed intoaddPattern
's arguments now just happens withinaddPattern
itself.The following flags are supported:
i
case insensitivem
multiline (^
and$
anchors adjacent to newlines)s
dot-all (.
matches newlines)I introduced a new option
applyRegexFlags
because, on inspection, it's not mutually exclusive topatternStrategy
: the former applies forz.string().regex()
, but latter applies only for.includes()
,.startsWith()
, and.endsWith()
. I've reworded the description forpatternStrategy
to highlight this a little more clearly—I honestly didn't properly understand it until I took a look at the code!To make the code cleaner, I also changed
zodPatterns
to be the full regexes from Zod (made case-insensitive). I also updated a couple that were out-of-date relative to Zod itself 😅Example
Caveats
/[Z-a]/
is a valid regex which would match"_"
but not"A"
. However,"A"
would be matched by/[Z-a]/i
; with my additions, this package would unfortunately transform it to/[z-aZ-A]/
, which would cause an error (range out of order) on parsing. If an invalid RegExp would be created, the package warns the user and falls back to the oldregex.source
./é/i
would not be transformed to/[éÉ]/
, it would be left unchanged. I don't know how to do this without going mad, so I haven't.Sorry for the delay; life suddenly got a little busy!
Resolves #116