Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe.each(
export const s_SimpleObject = z.object({
str: z.string(),
num: z.coerce.number(),
date: z.string(),
date: z.iso.date(),
datetime: z.iso.datetime({ offset: true }),
optional_str: z.string().optional(),
required_nullable: z.string().nullable(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,13 @@ export class ZodV4Builder extends AbstractSchemaBuilder<
)
}

// todo: add support for date, time
const base =
model.format === "date-time"
? "iso.datetime({offset:true})"
: model.format === "email"
? "email()"
: "string()"
[
["date-time", "iso.datetime({offset:true})"],
["date", "iso.date()"],
["time", "iso.time()"],
["email", "email()"],
].find((it) => it[0] === model.format)?.[1] ?? "string()"

return [
zod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ describe("typescript/common/schema-builders/zod-v4-schema-builder - unit tests",
)
await expect(execute("some string")).rejects.toThrow("Invalid email")
})

it("supports date-time", async () => {
const {code, execute} = await getActual(
ir.string({format: "date-time"}),
Expand All @@ -570,6 +571,25 @@ describe("typescript/common/schema-builders/zod-v4-schema-builder - unit tests",
"Invalid ISO datetime",
)
})

it("supports date", async () => {
const {code, execute} = await getActual(ir.string({format: "date"}))

expect(code).toMatchInlineSnapshot('"const x = z.iso.date()"')

await expect(execute("2024-05-25")).resolves.toBe("2024-05-25")
await expect(execute("some string")).rejects.toThrow("Invalid ISO date")
})

it("supports time", async () => {
const {code, execute} = await getActual(ir.string({format: "time"}))

expect(code).toMatchInlineSnapshot('"const x = z.iso.time()"')

await expect(execute("08:20:00")).resolves.toBe("08:20:00")
await expect(execute("some string")).rejects.toThrow("Invalid ISO time")
})

it("supports binary", async () => {
const {code} = await getActual(ir.string({format: "binary"}))

Expand Down