Skip to content

Zod: Support JSON-Schema minimum/exclusiveMinimum → Zod .min()/.gt() #2182

@danielArchlet

Description

@danielArchlet

🚀 Feature Request: Support JSON-Schema minimum/exclusiveMinimum → Zod .min()/.gt() refinements

Motivation
Currently Orval’s Zod client generates only the bare primitives (z.number(), z.string(), etc.) and does not translate JSON-Schema numeric keywords (minimum, exclusiveMinimum, maximum, exclusiveMaximum) into Zod refinements (.min(), .gt(), .max(), .lt()). This means that any numeric constraints declared in our OpenAPI definitions are lost in the generated schemas, leading to gaps between backend validation (e.g. Pydantic’s gt=0) and frontend validation in React-Hook-Form.

Current Behavior
Given an OpenAPI schema like:

components:
  schemas:
    BaseBid:
      type: object
      properties:
        price:
          type: number
          exclusiveMinimum: 0

Orval emits:

// generated by Orval Zod client
export const BaseBidSchema = z.object({
  price: z.number(),
});

—missing the .gt(0) that enforces exclusiveMinimum: 0.

Expected Behavior
Orval should detect numeric keywords and emit the corresponding Zod refinements. For the example above, the generated schema would be:

export const BaseBidSchema = z.object({
  price: z.number().gt(0),
});

Proposal
Extend the Zod client to map:

  • minimum: n → .min(n)
  • exclusiveMinimum: n → .gt(n)
  • maximum: n → .max(n)
  • exclusiveMaximum: n → .lt(n)

Thanks for the work on proper Zod refinements so far! Is support for these numeric refinements under consideration? Happy to help with test cases if that would be useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is neededzodZod related issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions