Open jansepke opened 3 months ago
You can do it cleaner with regex:
const schema = z
.string()
.regex(/^foo_|bar_|baz_/)
I'll admit that using .startsWith()
is a bit better because you can quickly see what it's intended to do, but the regex pattern will functionally cover your use-case.
@tristinDLC yes I know, but using regex always hides the intention. But I would also close this issue if this would add too much complexity for not enough value to zod.
I've submitted a PR to add support for multiple parameters in the .startsWith() and .endsWith() methods for string validation.
Currently it quite verbose to define a string that can start with multiple different prefixes:
it would be great if
.startsWith()
would support an array of strings likemaybe I am also missing an alternative solution for the problem.