When working with zod, you might have found yourself using .optional().nullable() to make a field optional and nullable.
Especially when you are working with tRPC or similar API libraries, this is a common case for API input validation.
But there is a better way to do this.
Instead of using .optional().nullable(), you can use .nullish().
const schema = z.object({
name: z.string().optional().nullable(), // ❌ don't do this
name: z.string().nullish(), // ✅ do this instead
});More Dev Tips
Discover more tips and tricks to level up your development skills
Stay up to date
Sign up for our newsletter and we will keep you updated on everything going on with supastarter.