Transformations
Custom Transformations
The Zod schema generator supports two ways of replacing the schema it would otherwise emit:
overrideFormats— a record keyed by OpenAPIformatstring. Use this to swap in a custom schema for every property with a given format (e.g.date-time,uuid).overrideSchema— a function that receives the full schema object and returns an override (ornullto fall through to the default behaviour). Use this when the decision depends on more than justformat.
Each override is an import describing a function that returns a Zod schema. The function is imported into the generated file and called at the override site.
date-time String to Temporal.Instant
import { defineConfig } from "@sohcah/openapi-generator/config";
import { createZodGenerator } from "@sohcah/openapi-generator/zod";
export default defineConfig({
schema: "swagger.json",
}).addBuilder("schemas", () =>
createZodGenerator({
output: "src/api.schemas.ts",
overrideFormats: {
"date-time": {
type: "import",
name: "zInstant",
from: "./api.helpers.js",
},
},
}),
);import { z } from "zod";
import { Temporal } from "@js-temporal/polyfill";
export const zInstant = () =>
z.codec(z.string(), z.instanceOf(Temporal.Instant), {
decode: (str) => Temporal.Instant.from(str),
encode: (date) => date.toString(),
});The from path is written into the generated file verbatim, so it should resolve from the location of the generator's output file (here, src/api.schemas.ts → ./api.helpers.js).