Capítulo 12 de 61
z.stringbool() (Zod 4+) converts "boolish" strings like "true", "1", "yes", "on" into a real boolean, which is the common case when parsing environment variables or form data.
z.stringbool(): parses recognized truthy/falsy strings into true/false; anything else throws an invalid_value error.["true", "1", "yes", "on", "y", "enabled"].["false", "0", "no", "off", "n", "disabled"].{ truthy, falsy }: override the accepted value lists.{ case: "sensitive" }: opts out of the default case-insensitive matching (inputs are lowercased before comparison by default).const strbool = z.stringbool();
strbool.parse("true"); // => true
strbool.parse("yes"); // => true
strbool.parse("off"); // => false
strbool.parse("anything else"); // throws ZodError (invalid_value)
// customizing accepted values
z.stringbool({
truthy: ["true", "1", "yes", "on", "y", "enabled"],
falsy: ["false", "0", "no", "off", "n", "disabled"],
});
z.stringbool() with z.coerce.boolean(): z.coerce.boolean() uses raw JS truthiness (any non-empty string is true, including "false"); z.stringbool() actually checks the string against a truthy/falsy word list, so "false" correctly maps to false.z.stringbool() is almost always the right choice over z.coerce.boolean() — it handles the "false" case correctly.{ case: "sensitive" } only when that's a real requirement.z.coerce.boolean()'s naive truthy/falsy behavior.