Strict boolean input
Validate a feature flag or request field that must already be a boolean.
Zod · Function
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.
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.
z.boolean()A Zod schema that validates boolean values.
z.boolean() to declare a schema whose intended inputs are booleans.true succeeds and returns true.false succeeds and returns false.Boolean(input) conversion.z.boolean() when the input should be converted; choose z.coerce.boolean() for coercion.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.
Validate a feature flag or request field that must already be a boolean.
Check parsed configuration values after the application has already converted them to booleans.
Compose a boolean field inside larger Zod object schemas.
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".
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.
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