Parse environment variables
Environment variables and configuration values arrive as strings but application logic needs actual booleans.
Zod · Module
z.stringbool() parses supported boolean-like strings into plain booleans. It is useful for text-based inputs such as environment variables and can encode booleans back to strings.
z.stringbool() creates a schema that interprets supported string values as booleans. It is useful when inputs arrive as text, such as environment variables; parsing produces a plain boolean, while encoding can convert booleans back to their string forms.
z.stringbool()A Zod codec/schema that parses supported string values into booleans and encodes booleans into strings.
import { z } from "zod";
const enabled = z.stringbool();
console.log(enabled.parse("yes"));
console.log(enabled.parse("0"));Converts supported string representations into booleans when parsing.
Environment variables and configuration values arrive as strings but application logic needs actual booleans.
A boundary receives values such as "true", "1", "yes", or "on" and should normalize them to booleans.
A codec pipeline needs to turn booleans back into their default string representations for output.
z.stringbool() defines a schema that converts recognized string representations such as "true", "false", "yes", and "no" into booleans. Parsing a valid string returns a plain boolean.
Follow-up: How can the conversion be reversed? Use the codec's encoding operation: true becomes "true" and false becomes "false" by default.
It is especially useful for text-based inputs such as environment variables, where boolean-like values arrive as strings. The schema parses those values into boolean data for application code.
Follow-up: Why not use z.boolean()? A regular boolean schema expects boolean input, whereas stringbool handles boolean-like text.
The API supports both decoding and encoding because it is internally implemented as a codec. decode("true") returns true, while encode(true) returns "true" with the default configuration.
Follow-up: What default output should be expected when encoding false? The default encoded value is the string "false".
z.boolean()z.encode()z.decode()codecs