Capítulo 47 de 61
z.toJSONSchema() normally returns one self-contained schema, but passing a registry (instead of a single schema) produces an interlinked set of schemas keyed by id, using $ref to reference each other — the right approach for exporting mutually-referencing schemas as separate .json files or endpoints.
z.globalRegistry.add(schema, { id }): registers a schema with an id used as its key/reference name in multi-schema output. Schemas without a registered id are silently ignored when converting a registry.z.toJSONSchema(registry): returns { schemas: { [id]: JSONSchema } }, with cross-references between registered schemas rendered as $ref pointing at the other schema's id.uri option: by default $refs are relative ("User"); pass uri: (id) => \https://example.com/${id}.json\`` to produce fully-qualified reference URIs, e.g. for serving each schema from its own endpoint.const User = z.object({
name: z.string(),
get posts() { return z.array(Post); },
});
const Post = z.object({
title: z.string(),
content: z.string(),
get author() { return User; },
});
z.globalRegistry.add(User, { id: "User" });
z.globalRegistry.add(Post, { id: "Post" });
z.toJSONSchema(z.globalRegistry);
// => { schemas: {
// User: { id: "User", type: "object", properties: { name: {...}, posts: { type: "array", items: { $ref: "Post" } } }, ... },
// Post: { id: "Post", type: "object", properties: { title: {...}, content: {...}, author: { $ref: "User" } }, ... },
// } }
z.toJSONSchema(z.globalRegistry, { uri: (id) => `https://example.com/${id}.json` });
// => same shape, but $refs become "https://example.com/User.json" etc.
id on every schema you expect in the multi-schema output: unregistered schemas are silently dropped — not an error, just absent from the result.z.toJSONSchema() for self-contained output, and the registry form specifically when schemas reference each other and need to become separate, linkable documents.id — there's no implicit naming from variable names.uri option is what turns relative $refs into absolute URLs suitable for a schema actually served over HTTP.User/Post here.