title: Zod Schema Maybe Type Implementation category: PUBLIC/Tech/Development tags:
- likely-copied
- public-candidate
- complete
- medium-priority llm_summary: Implementation of maybe type in Zod schema validation library description: Code example showing how to implement a maybe type in Zod schema validation, combining optional and nullable types date: 2023-11-29 source_folder: web-research
Not a fan of this for reasons I describe briefly in #403
In your particular case, you could implement a maybe function to avoid some verbosity:
const maybe = <T extends z.ZodTypeAny>(schema: T)=>{
return schema.optional().nullable();
};
// equivalent
maybe(z.string());
z.string().optional().nullable();Source Feature request: “.maybe()” type · Issue #391 · colinhackszod at 2023-11-29.