zod icon indicating copy to clipboard operation
zod copied to clipboard

Feature_request: Conditional Schema Validation

Open Michealjunior opened this issue 3 weeks ago • 2 comments

Problem Brief

Zod lacks conditional validation for dynamic schema behavior based on field values or context. Users need dependent field validation where one field's requirements change based on another field's value, similar to Yup's .when() method. This forces complex workarounds using refine or multiple separate schemas.

Agent Instructions

Implement a .when() method on ZodType that accepts dependency fields and condition functions. Return different schemas based on field values or context data. Support both single-field and multi-field dependencies. Handle circular dependencies by throwing errors. Preserve immutability by returning new schema instances. Ensure type inference reflects conditional output types. Maintain backward compatibility - existing schemas without .when() continue working unchanged.

Test Assumptions

Method signature: schema.when(dependency: string | string[], condition: (value: any, context: any) => Schema | boolean)

Michealjunior avatar Nov 27 '25 12:11 Michealjunior

Duplicate Detection

ℹ️ This issue may be similar to colinhacks/zod#3874.

Related Issues

  • colinhacks/zod#3874: Feature Request: Yup-like Conditional Schema Validation with .when()

Suggested Issue Assignees

  • 9nitK

Actions

  • [ ] 🗺️ Generate an implementation plan for this issue

ℹ️ Note: Issue enrichment is currently in early access.

Disable automatic issue enrichment

To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

issue_enrichment:
  auto_enrich:
    enabled: false

coderabbitai[bot] avatar Nov 27 '25 12:11 coderabbitai[bot]

hey @Michealjunior , we have to provide only implementation plan for this issue??

Dhruv-2403 avatar Nov 30 '25 18:11 Dhruv-2403