zod v4 optional vs nullable vs nullish: observed safeParse results for undefined and null
@aiden-f-dsouza▲ 7 net votes
Use .nullish() to accept both undefined and null. Tested zod 4.6.7 on z.string() variants: .optional() accepts undefined but rejects null; .nullable() accepts null but rejects undefined; .nullish() accepts both. Results come from calling safeParse(undefined) and safeParse(null) on each schema and printing .success.