8000 z.looseEnum() for more permissive enum validation · Issue #4362 · colinhacks/zod · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

z.looseEnum() for more permissive enum validation #4362

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

Open
marco-carvalho opened this issue May 13, 2025 · 5 comments · May be fixed by #4391
Open

z.looseEnum() for more permissive enum validation #4362

marco-carvalho opened this issue May 13, 2025 · 5 comments · May be fixed by #4391

Comments

@marco-carvalho
Copy link

Would it be possible to introduce a z.looseEnum() that functions like z.enum(), but with more forgiving input validation? In my current workflow, I follow a request/model/response schema pattern. While models and responses are strictly typed, I want my request objects to be more permissive. Currently, I work around this by doing something like:

z.enum(['foo', 'bar']).or(z.string())

A dedicated z.looseEnum() could allow unknown values through in request schemas.

Examples:

Request:

const RequestSchema = z.looseObject({
  status: z.looseEnum(['pending', 'cancelled', 'done']),
});

Model:

const ModelSchema = z.strictObject({
  status: z.enum(['pending', 'cancelled', 'done']),
});

Response:

const ResponseSchema = z.strictObject({
  status: z.enum(['pending', 'cancelled', 'done']),
});
@colinhacks
Copy link
Owner

It would be nice if there was a way to define a schema with the inferred type "pending" | "cancelled" | (string & {}). That type signature gives you autocompletion but still allows arbitrary strings. This is an interesting proposal for achieving that.

8000
@Lavdewangan1
Copy link

Hello @colinhacks, I would like to work on this issue and create this function.

@marco-carvalho
Copy link
Author

@Lavdewangan1 maybe you should work in v4 branch?

@Lavdewangan1
Copy link

@marco-carvalho I am a bit unfamiliar with the v4 codebase can you please tell me the files and directories I've to add in the codebase in order to implement this feature correctly, it will be really helpfull if you can.

@marco-carvalho
Copy link
Author

@Lavdewangan1 I'm just as unfamiliar with the v4 codebase as you are 😅

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 a pull request may close this issue.

3 participants
0