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

Zod · Function

z.boolean

z.boolean() creates a Zod schema for validating boolean values. It accepts true and false; use z.coerce.boolean() when JavaScript-style coercion is desired.

How it works

z.boolean() creates a schema for primitive boolean values. Parsing succeeds for true or false; inputs of other types fail validation. Use z.coerce.boolean() instead when conversion is intended, noting that conversion follows JavaScript truthiness.

Syntax

z.boolean()

Returns

A Zod schema that validates boolean values.

Key points

  • Use z.boolean() to declare a schema whose intended inputs are booleans.
  • Parsing true succeeds and returns true.
  • Parsing false succeeds and returns false.
  • The coercing counterpart applies the JavaScript Boolean(input) conversion.

Common mistakes

  • Do not use z.boolean() when the input should be converted; choose z.coerce.boolean() for coercion.

Example: Validate boolean values

import { z } from "zod";

const enabled = z.boolean();

console.log(enabled.parse(true));
console.log(enabled.parse(false));

try {
  enabled.parse("true");
} catch (error) {
  console.log("Not a boolean");
}

Creates a boolean schema, accepts both boolean values, and rejects a string.

When to use it

Strict boolean input

Validate a feature flag or request field that must already be a boolean.

Configuration validation

Check parsed configuration values after the application has already converted them to booleans.

Object-schema fields

Compose a boolean field inside larger Zod object schemas.

Interview questions

QWhat does z.boolean() validate?

z.boolean() returns a schema that validates primitive boolean values. Calling .parse(true) or .parse(false) returns the corresponding value; a value of another type fails validation.

Follow-up: How would you handle string or numeric input? Use z.coerce.boolean(), but first account for its JavaScript truthiness behavior rather than expecting semantic parsing of text such as "false".

QHow does z.boolean() differ from z.coerce.boolean()?

The schema is strict about the input type: it accepts true and false as booleans. It does not convert other values during parsing.

Follow-up: What alternative performs conversion? z.coerce.boolean() applies the built-in Boolean conversion to the input.

QHow do you use a boolean schema at runtime?

A boolean schema can be parsed with the standard parsing API, as shown by z.boolean().parse(true) and z.boolean().parse(false). Successful parsing returns the validated boolean.

Follow-up: What should be used if an environment variable needs configurable textual true/false values? Zod 4 provides z.stringbool() for customized truthy and falsy strings.

  • z.coerce.boolean()
  • z.stringbool()
  • parse

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