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

feat: single method to comine .unwrap().unwrap() #3456

Closed
juliusmarminge opened this issue May 2, 2024 · 1 comment
Closed

feat: single method to comine .unwrap().unwrap() #3456

juliusmarminge opened this issue May 2, 2024 · 1 comment

Comments

@juliusmarminge
Copy link

Currently, if you have a schema with both optional and nullable, you have to call unwrap twice in order to get the base type. It would be nice to get it immediately.

I propose adding something like .nonnullable() (to match TypeScript's NonNullable<T> type) that does this in one shot:

Sandbox: https://stackblitz.com/edit/vitejs-vite-ursctp?file=src%2Fmain.ts

@colinhacks
Copy link
Owner

My current thinking on this is described here: #3141 (comment)

If Zod ever did implemented .nonNullable() it would actually wrap the current schema in another ZodNonNullable schema, instead of unwrapping, because it needs to work in conjunction with transforms, refinements, .or(z.undefined()). e.g.

z.string().or(z.null()).refine(()=>true).transform(val => Math.random() > 0.5 ? val : undefined).nonNullable()

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

2 participants