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

Zod · Module

z.stringbool

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.

Since
4.0

How it works

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.

Syntax

z.stringbool()

Returns

A Zod codec/schema that parses supported string values into booleans and encodes booleans into strings.

Key points

  • The API was introduced in Zod 4.0.
  • It targets text inputs that represent booleans, especially configuration and environment-variable values.
  • The text "true" parses to the boolean true.
  • The text "1" is accepted as a truthy representation and parses to true.
  • The text "on" is another supported truthy representation.
  • The text "enabled" is accepted as a truthy representation.

Common mistakes

  • Encoding uses the default string spellings "true" and "false" for boolean values.
  • This schema is for string input; it converts recognized text into booleans rather than accepting arbitrary values.

Example: Parse a string boolean

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.

When to use it

Parse environment variables

Environment variables and configuration values arrive as strings but application logic needs actual booleans.

Normalize boolean-like text

A boundary receives values such as "true", "1", "yes", or "on" and should normalize them to booleans.

Encode booleans as strings

A codec pipeline needs to turn booleans back into their default string representations for output.

Interview questions

QWhat does z.stringbool() do?

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.

QWhen would you use z.stringbool() instead of z.boolean()?

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.

QHow does z.stringbool() behave as a codec?

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

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