-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
nonNullable to disallow null #3141
Conversation
✅ Deploy Preview for guileless-rolypoly-866f8a ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
I appreciate the PR, but this is a little too niche to go into Zod core. This is better implemented with a transform. const A = z.string().nullable().transform((val, ctx) => {
if(val === null) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Cannot be null",
path: [],
})
return z.NEVER;
}
return val;
})
type A = z.infer<typeof A>;
// string |
I obviuosly disagree, otherwise I wouldn't have made the PR. It would have come handy at work when I decided to work on this. Furthermore it is compliant with typescripts NonNullable, and not a funky Idea that I just made up. So if NonNullable would ever be a thing, it would be in the way this PR proposes. The typescript developers didn't think a NonNullable type is too niche to add it to typescript. Transformers are flexible, but they are not nearly as readable and beautiful like the zods other features. And also, this addition already has automated tests and is fully documented. This is a finished feature that doesn't add immediate development overhead for you. It's as good as it gets. |
I think zod should be made as similar to typescripts types as possible. I want to be able to replace all interfaces and such with zod types with beautiful zod structures. Therefore, I think as many of typescripts built ins (like Well, my 2 cents. |
I guess I should also add that I didn't encounter a situation in which I wanted to use this since making the PR, maybe someone else should comment on this instead. I can't give you a realistic estimate on how commonly this feature would be used. On the other hand, there are tons of other existing zod features that I didn't consider using since then as well, so... |
What's more is the transform workaround here doesn't seem to work for me with const schema = z.object({
nestedValue: z
.any()
.nullable()
.transform((val) => (val == null ? z.NEVER : val)),
})
type SchemaType = z.infer<typeof schema> type SchemaType = {
nestedValue?: any;
} |
nonNullable also restricts
That aged well committed the change. use case: A gateway passes along all payloads it gets, and it doesn't care for their content, but it does check if the payloads are set by using |
The fact that |
can both be now used to make this throw:
This is not equivalent to unwrap (as proposed in #2190). Instead, this is equivalent to typescripts NonNullable type https://www.typescriptlang.org/docs/handbook/utility-types.html#nonnullabletype