Capítulo 31 de 61
z.json() is a convenience schema for "any JSON-encodable value" — a recursive union covering every JSON type.
z.json(): validates any value that could round-trip through JSON.stringify/JSON.parse — string, number, boolean, null, array of JSON values, or a string-keyed record of JSON values.z.lazy() union: internally equivalent to a self-referential union of primitives, arrays, and records, built with z.lazy() to allow the recursive reference.const jsonSchema = z.json();
// conceptually equivalent to:
const jsonSchemaExpanded = z.lazy(() =>
z.union([
z.string(),
z.number(),
z.boolean(),
z.null(),
z.array(jsonSchemaExpanded),
z.record(z.string(), jsonSchemaExpanded),
])
);
z.json() is sugar over a recursive union you could otherwise hand-write.z.json() whenever a field must be "anything JSON-serializable" (e.g. a generic metadata blob), instead of hand-rolling the recursive union.z.json() can validate arbitrarily nested JSON structures, not just flat values.