Repository navigation
Replies: 2 comments 1 reply
|
In Zod v4, descriptions and other metadata are no longer stored directly on schema fields, so const schema = z.object({
name: z.string().describe("User's name"),
});
// Access the description:
const desc = schema.shape.name.meta()?.description; // "User's name"If you've chained methods like const nameSchema = z.string().describe("User's name").optional();
const desc = nameSchema.unwrap().meta()?.description; // "User's name"You can also retrieve descriptions from the JSON Schema output using More details in the Zod v4 metadata docs and related GitHub issue. If this answers your question, feel free to close the issue! To reply, just mention @dosu. Docs are dead. Just use Dosu. |
|
That still works — The part that bites is that the instance has to be the one you called const S = z.object({
a: z.string().describe("desc"),
b: z.string().describe("desc").optional(),
c: z.string().optional().describe("desc"),
});
S.shape.a.description; // "desc"
S.shape.b.description; // undefined <- the description is on the string, not the optional
S.shape.c.description; // "desc"
function describedAs(schema: z.core.$ZodType): string | undefined {
let cur: any = schema;
while (cur) {
if (cur.description !== undefined) return cur.description;
cur = cur._zod.def.innerType;
}
return undefined;
}
describedAs(S.shape.b); // "desc"Worth knowing that z.toJSONSchema(S, { io: "input" }).properties;
// a: { type: "string", description: "desc" }
// b: { type: "string", description: "desc" }
// c: { description: "desc", type: "string" }Two smaller things. |
Uh oh!
There was an error while loading. Please reload this page.
in v3 I used to do something like
in order to access the description assigned to a field using
.describewhat is the equivalent in v4All reactions