You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Zod currently lacks an elegant way to handle conditional validation similar to Yup's .when() method. This makes it challenging to implement dependent field validations, especially in complex forms where validation rules depend on other field values.
Current Solutions and Limitations
1. Using .refine()
// Current approach using refineconstschema=z.object({employmentType: z.enum(['employed','self-employed','unemployed']),companyName: z.string().refine((val,ctx)=>{constemploymentType=ctx.parent.employmentType;if(employmentType==='employed'&&!val){returnfalse;}returntrue;},{message: 'Company name is required for employed individuals'})});
2. Using .superRefine()
constschema=z.object({employmentType: z.enum(['employed','self-employed','unemployed']),companyName: z.string(),}).superRefine((data,ctx)=>{if(data.employmentType==='employed'&&!data.companyName){ctx.addIssue({code: z.ZodIssueCode.custom,message: "Company name is required for employed individuals",path: ["companyName"]});}});
3. Using Separate Schemas with Union
constemployedSchema=z.object({employmentType: z.literal('employed'),companyName: z.string().min(1,"Company name is required")});constunemployedSchema=z.object({employmentType: z.literal('unemployed'),companyName: z.string().optional()});constschema=z.discriminatedUnion("employmentType",[employedSchema,unemployedSchema]);
Limitations of Current Approaches
Verbose and complex code
Less readable and maintainable
Harder to reuse validation logic
Not as intuitive as Yup's approach
Difficult to handle multiple dependent fields
Proposed Solution
1. Basic .when() Implementation
// Simple field dependencyconstschema=z.object({employmentType: z.enum(['employed','self-employed','unemployed']),companyName: z.string().when('employmentType',{is: 'employed',then: (schema)=>schema.min(1,"Company name is required"),otherwise: (schema)=>schema.optional()})});
2. Multiple Field Dependencies
// Multiple field dependenciesconstschema=z.object({country: z.string(),state: z.string(),zipCode: z.string().when(['country','state'],{is: ({country, state})=>country==='US'&&state==='CA',then: (schema)=>schema.regex(/^\d{5}$/,"Must be 5 digits for US/CA"),otherwise: (schema)=>schema.optional()})});
3. Array-based Validation
// Array validationconstschema=z.object({hasPhones: z.boolean(),phones: z.array(z.string()).when('hasPhones',{is: true,then: (schema)=>schema.min(1,"At least one phone required"),otherwise: (schema)=>schema.max(0)})});
Proposal: Add Yup-like Conditional Schema Validation (.when())
Problem Statement
Zod currently lacks an elegant way to handle conditional validation similar to Yup's
.when()
method. This makes it challenging to implement dependent field validations, especially in complex forms where validation rules depend on other field values.Current Solutions and Limitations
1. Using
.refine()
2. Using
.superRefine()
3. Using Separate Schemas with Union
Limitations of Current Approaches
Proposed Solution
1. Basic
.when()
Implementation2. Multiple Field Dependencies
3. Array-based Validation
4. Chaining Multiple Conditions
Type Safety
Implementation Considerations
Path Resolution:
Type Inference:
Performance:
Examples of Complex Use Cases
1. Form with Dynamic Sections
2. Multi-step Validation
Benefits
Next Steps
Questions for Discussion
.when()
conditions?.when()
conditions on the same field?The text was updated successfully, but these errors were encountered: