Skip to content

Commit e7eb164

Browse files
authored
feat: support format: date / time (#467)
1 parent 49683d4 commit e7eb164

7 files changed

Lines changed: 35 additions & 15 deletions

File tree

integration-tests/typescript-express/src/generated/api.github.com.yaml/generated.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration-tests/typescript-express/src/generated/api.github.com.yaml/schemas.ts

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration-tests/typescript-koa/src/generated/api.github.com.yaml/generated.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration-tests/typescript-koa/src/generated/api.github.com.yaml/schemas.ts

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-code-generator/src/typescript/common/schema-builders/zod-v4-schema-builder.integration.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe.each(
4343
export const s_SimpleObject = z.object({
4444
str: z.string(),
4545
num: z.coerce.number(),
46-
date: z.string(),
46+
date: z.iso.date(),
4747
datetime: z.iso.datetime({ offset: true }),
4848
optional_str: z.string().optional(),
4949
required_nullable: z.string().nullable(),

packages/openapi-code-generator/src/typescript/common/schema-builders/zod-v4-schema-builder.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,13 @@ export class ZodV4Builder extends AbstractSchemaBuilder<
329329
)
330330
}
331331

332-
// todo: add support for date, time
333332
const base =
334-
model.format === "date-time"
335-
? "iso.datetime({offset:true})"
336-
: model.format === "email"
337-
? "email()"
338-
: "string()"
333+
[
334+
["date-time", "iso.datetime({offset:true})"],
335+
["date", "iso.date()"],
336+
["time", "iso.time()"],
337+
["email", "email()"],
338+
].find((it) => it[0] === model.format)?.[1] ?? "string()"
339339

340340
return [
341341
zod,

packages/openapi-code-generator/src/typescript/common/schema-builders/zod-v4-schema-builder.unit.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ describe("typescript/common/schema-builders/zod-v4-schema-builder - unit tests",
554554
)
555555
await expect(execute("some string")).rejects.toThrow("Invalid email")
556556
})
557+
557558
it("supports date-time", async () => {
558559
const {code, execute} = await getActual(
559560
ir.string({format: "date-time"}),
@@ -570,6 +571,25 @@ describe("typescript/common/schema-builders/zod-v4-schema-builder - unit tests",
570571
"Invalid ISO datetime",
571572
)
572573
})
574+
575+
it("supports date", async () => {
576+
const {code, execute} = await getActual(ir.string({format: "date"}))
577+
578+
expect(code).toMatchInlineSnapshot('"const x = z.iso.date()"')
579+
580+
await expect(execute("2024-05-25")).resolves.toBe("2024-05-25")
581+
await expect(execute("some string")).rejects.toThrow("Invalid ISO date")
582+
})
583+
584+
it("supports time", async () => {
585+
const {code, execute} = await getActual(ir.string({format: "time"}))
586+
587+
expect(code).toMatchInlineSnapshot('"const x = z.iso.time()"')
588+
589+
await expect(execute("08:20:00")).resolves.toBe("08:20:00")
590+
await expect(execute("some string")).rejects.toThrow("Invalid ISO time")
591+
})
592+
573593
it("supports binary", async () => {
574594
const {code} = await getActual(ir.string({format: "binary"}))
575595

0 commit comments

Comments
 (0)