Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Zod · Module

z.null

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.

How it works

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.

Syntax

z.null()

Returns

A Zod schema that accepts the JavaScript value null and rejects other values.

Key points

  • The schema targets the JavaScript literal null, rather than the text value "null".
  • z.null() is distinct from z.undefined() and z.void(), which concern undefined instead.
  • When converted to JSON Schema, the schema is represented with the single type null.

Common mistakes

  • Do not confuse z.null() with a nullable schema: it accepts only null, whereas z.nullable(schema) combines null with the wrapped schema's accepted values.
  • Use a nullable wrapper when null should be added to an existing schema; z.null() itself does not preserve or add another schema's values.

Example: Validate a null value

import { z } from "zod";

const NullValue = z.null();

console.log(NullValue.parse(null)); // null
console.log(NullValue.safeParse("null").success); // false

Creates a schema that accepts the JavaScript value null and rejects other values.

When to use it

Strict null fields

Validate an API or configuration field that must explicitly contain null.

Null sentinel values

Model a discriminated or sentinel state whose value is exactly null.

Schema generation

Generate JSON Schema for a value restricted to null.

Interview questions

QWhat does 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().

QWhen should you use 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).

QHow is 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()

Source: Defining schemas | Zod. Checked 2026-10-15.