Skip to content
Open
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
2 changes: 2 additions & 0 deletions typescript/src/zod/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ export function getZodSchemaAsTypeScript(schema: Record<string, z.ZodType>): str
return appendUnionOrIntersectionTypes([(type._zod.def as z.core.$ZodOptionalDef).innerType as z.ZodType, z.undefined()], TypePrecedence.Union);
case "readonly":
return appendReadonlyType(type);
case "pipe":
return appendType((type._zod.def as z.core.$ZodPipeDef).in as z.ZodType);
}
append("any");
}
Expand Down
35 changes: 35 additions & 0 deletions typescript/test/zod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,41 @@ describe("getZodSchemaAsTypeScript", () => {

});

// -----------------------------------------------------------------------
// Input schemas for transformations and pipelines
// -----------------------------------------------------------------------

describe("z.transform() and z.pipe()", () => {

it("emits the JSON input type for a transform pipeline", () => {
const DateTime = z.iso.datetime().transform(value => new Date(value));
assert.match(schemaOf("DateTime", DateTime), /type DateTime = string;/);
});

it("emits the input schema for a pipeline", () => {
const NumberFromString = z.string().pipe(z.coerce.number());
assert.match(schemaOf("NumberFromString", NumberFromString), /type NumberFromString = string;/);
});

it("preserves transformed runtime values during validation", () => {
const Event = {
Event: z.object({
dateTime: z.iso.datetime().transform(value => new Date(value)),
}),
};
const validator = createZodJsonValidator(Event, "Event");
assert.match(validator.getSchemaText(), /dateTime: string;/);

const result = validator.validate({ dateTime: "2022-08-20T08:30:00.000Z" });
assert.ok(result.success, "expected ISO date-time validation to succeed");
assert.ok(result.data.dateTime instanceof Date);

const invalidResult = validator.validate({ dateTime: "not-a-date" });
assert.ok(!invalidResult.success, "expected invalid date-time validation to fail");
});

});

// -----------------------------------------------------------------------
// Fallthrough: unknown type kind → any
// -----------------------------------------------------------------------
Expand Down