Skip to content

[Zod plugin] Allow code generation customization #2688

@IAluI

Description

@IAluI

Description

Summary

Proposes adding new settings to the @hey-api/zod plugin to allow greater customization of code generation.

Motivation

Currently, the only available option that influences code generation is dates, which provides very limited control. Adding new settings would allow:

  • Ability to use other Zod functions, such as codec.
  • Ability to refine constraints defined in OpenAPI, e.g., format: int32z.int32().
  • Ability to specify constraints based on OpenAPI Extensions.

These improvements would give developers more flexibility and precision when generating Zod schemas from OpenAPI specifications.

Proposed Solution

1. Add the schemaOverride Setting

schemaOverride is a function called in schemaTypeToZodSchema that allows developers to override the schemas generated by default.

2. Add the customSchemaBuilder Setting

customSchemaBuilder is a function called in the handler that allows creating reusable custom schemas. These can then be used in schemaOverride to avoid code duplication.

Proof of concept

In this fork, I crudely modified @hey-api/zod by adding Zod codecs to convert dates represented as strings into Dayjs objects.

The openapi-ts-sample example demonstrates how the received data is transformed using the created schemas.

Opportunities for Further Development

With Zod, the @hey-api/sdk plugin configuration could be extended to not only catch errors during type coercion but also return transformed data directly.

  • someSchema.decode(data) — for validating and transforming response bodies.
  • someSchema.encode(data) — for preparing request payloads.

This enables reusing the same schema for both validation and data transformation, reducing code duplication and improving reliability.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions