Basic Usage
import { z } from 'zod';
const schema = z.boolean();
schema.parse(true); // true
schema.parse(false); // false
schema.parse("true"); // throws ZodError
schema.parse(1); // throws ZodError
Type Signature
function boolean(params?: string | $ZodBooleanParams): ZodBoolean
params
string | $ZodBooleanParams
Optional error message (string) or configuration object
Behavior
z.boolean() only accepts literal true and false values. It does NOT perform type coercion:
const schema = z.boolean();
// Valid
schema.parse(true); // true
schema.parse(false); // false
// Invalid - throws ZodError
schema.parse("true");
schema.parse("false");
schema.parse(1);
schema.parse(0);
schema.parse(null);
schema.parse(undefined);
Type Coercion
If you need to accept truthy/falsy values, use coercion:
import { z } from 'zod';
const schema = z.coerce.boolean();
schema.parse(true); // true
schema.parse(false); // false
schema.parse("true"); // true
schema.parse("false"); // false
schema.parse(1); // true
schema.parse(0); // false
schema.parse("yes"); // true
schema.parse(""); // false
Usage with Optional
const optionalBoolean = z.boolean().optional();
optionalBoolean.parse(true); // true
optionalBoolean.parse(false); // false
optionalBoolean.parse(undefined); // undefined
Usage with Nullable
const nullableBoolean = z.boolean().nullable();
nullableBoolean.parse(true); // true
nullableBoolean.parse(false); // false
nullableBoolean.parse(null); // null
Default Values
const withDefault = z.boolean().default(false);
withDefault.parse(true); // true
withDefault.parse(undefined); // false (default applied)
Custom Error Messages
const schema = z.boolean("Must be a boolean value");
// Or with object syntax
const schema2 = z.boolean({
message: "Must be a boolean value"
});
TypeScript Type
import { z } from 'zod';
const schema = z.boolean();
type BooleanType = z.infer<typeof schema>; // boolean
Refinements
While uncommon, you can add refinements to boolean schemas:
const mustBeTrue = z.boolean().refine(val => val === true, {
message: "Value must be true"
});
mustBeTrue.parse(true); // true
mustBeTrue.parse(false); // throws
See Also