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
-
I've been using Zod for API validation in corporate projects, and handling
null
has 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
null
an Issue?In many programming languages,
undefined
doesn’t exist, but in JavaScript,null
is often used as an intentional empty value. However, when dealing with APIs, 99% of thenull
values 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 withnullish
repeatedly 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