Strict null fields
Validate an API or configuration field that must explicitly contain null.
Zod · Module
z.null() creates a Zod schema that accepts exactly the JavaScript value null. Use z.nullable() or z.nullish() when null should be combined with other accepted values.
z.null() constructs a schema for the JavaScript null literal. Parsing succeeds only when the input is null; use z.nullable() when another schema's values should also be accepted, or z.nullish() when both null and undefined should be allowed.
z.null()A Zod schema that accepts the JavaScript value null and rejects other values.
null, rather than the text value "null".z.null() is distinct from z.undefined() and z.void(), which concern undefined instead.null.z.null() with a nullable schema: it accepts only null, whereas z.nullable(schema) combines null with the wrapped schema's accepted values.z.null() itself does not preserve or add another schema's values.import { z } from "zod";
const NullValue = z.null();
console.log(NullValue.parse(null)); // null
console.log(NullValue.safeParse("null").success); // falseCreates a schema that accepts the JavaScript value null and rejects other values.
Validate an API or configuration field that must explicitly contain null.
Model a discriminated or sentinel state whose value is exactly null.
Generate JSON Schema for a value restricted to null.
z.null() validate?z.null() is a Zod schema whose accepted value is the JavaScript literal null. Its inferred TypeScript output is therefore null, and parsing any non-null value fails.
Follow-up: How would you accept either a string or null? Use z.nullable(z.string()), or the equivalent z.string().nullable().
z.null() instead of z.nullable()?Use z.null() for a field or value that must explicitly be null. Use z.nullable(schema) when the wrapped schema's values and null should both be valid.
Follow-up: What is the distinction from a nullish schema? A nullish schema permits both undefined and null, typically through z.nullish(schema).
z.null() represented in JSON Schema?Zod documents z.null() as the schema for the JavaScript null literal. In JSON Schema conversion, it becomes an object with type: "null".
Follow-up: What happens for a nullable string in JSON Schema? A simple nullable string can use type: ["string", "null"]; more complex inner schemas may use anyOf.
z.undefined()z.void()z.nullable()z.nullish()z.literal()