Simplifying 'null' Handling in Zod with a Cleaner Approach #4000
semanticist21
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I've been using Zod for API validation in corporate projects, and handling
nullhas always been a frustrating experience. I believe Zod should provide a shorthand function for.nullish().transform(x => x ?? undefined)to make this process cleaner.Why is
nullan Issue?In many programming languages,
undefineddoesnβt exist, but in JavaScript,nullis often used as an intentional empty value. However, when dealing with APIs, 99% of thenullvalues I encounter are just placeholders and should be treated asundefined.When defining optional fields in API specifications,
.nullish()helps, but it doesnβt fully resolve the issue. Since I rely on Zod for most preprocessing and postprocessing, dealing withnullishrepeatedly becomes a tiresome task.Problems with the Current Approach
1. Repetitive
.transform()CallsEvery time I use
.default(), I also need to manually apply.transform(). Imagine doing this across 50+ files with complex data structures.2. Type Compatibility Issues
Null values also introduce inconsistencies when working with TypeScript interfaces.
Beta Was this translation helpful? Give feedback.
All reactions