boolean()
Creates a validator that accepts boolean values. Supports string and numeric coercion.
Signature
function boolean(): BooleanValidatorAccepted Values
| Input | Result | Notes |
|---|---|---|---|
| true | ✅ true | Actual boolean |
| false | ✅ false | Actual boolean |
| "true" | ✅ true | Case-insensitive, trimmed |
| "false" | ✅ false | Case-insensitive, trimmed |
| "TRUE" | ✅ true | Case-insensitive |
| "1" | ✅ true | String "1" |
| "0" | ✅ false | String "0" |
| "yes" | ✅ true | Case-insensitive |
| "no" | ✅ false | Case-insensitive |
| "on" | ✅ true | Case-insensitive |
| "off" | ✅ false | Case-insensitive |
| 1 | ✅ true | Number 1 |
| 0 | ✅ false | Number 0 |
| 2 | ❌ Type error | Only 1 and 0 are accepted |
| null | ❌ Type error | |
Refinements
boolean() has no type-specific refinements. It only has the
chainable methods (.optional(), .default(), .describe(),
.secret(), .validate()).
Examples
Basic boolean
const env = defineEnv({
DEBUG: boolean(),
})
// With process.env.DEBUG = "true"
// env.DEBUG → trueOptional boolean
const env = defineEnv({
FEATURE_X_ENABLED: boolean().optional(),
})
// env.FEATURE_X_ENABLED: boolean | undefinedBoolean with default
const env = defineEnv({
ENABLE_LOGGING: boolean().default(true),
})
// env.ENABLE_LOGGING: boolean (defaults to true if not set)Common patterns
const env = defineEnv({
// Feature flags
ENABLE_NEW_DASHBOARD: boolean().default(false),
SHOW_EXPERIMENTAL: boolean().optional(),
// Logging controls
DEBUG: boolean().default(false).describe("Enable debug logging"),
VERBOSE: boolean().default(false),
})