Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: support multiple parameters in .startsWith() #3683

Open
jansepke opened this issue Jul 30, 2024 · 3 comments
Open

Feature: support multiple parameters in .startsWith() #3683

jansepke opened this issue Jul 30, 2024 · 3 comments

Comments

@jansepke
Copy link

Currently it quite verbose to define a string that can start with multiple different prefixes:

const schema = z
  .string()
  .startsWith('foo_')
  .or(z.string().startsWith('bar_'))
  .or(z.string().startsWith('baz_'));

it would be great if .startsWith() would support an array of strings like

const schema = z
  .string()
  .startsWith(['foo_', 'bar_', 'baz_']);

maybe I am also missing an alternative solution for the problem.

@tristinDLC
Copy link

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.

@jansepke
Copy link
Author

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

@Kumar06Lav
Copy link

I've submitted a PR to add support for multiple parameters in the .startsWith() and .endsWith() methods for string validation.

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

No branches or pull requests

3 participants