Validators are chainable builder objects that describe the type, constraints, and fallback behavior of a single environment variable. You compose them inline inside yourDocumentation Index
Fetch the complete documentation index at: https://next-safe-env.dev/llms.txt
Use this file to discover all available pages before exploring further.
server and client schemas - each method returns the same validator instance so calls can be chained freely. Every validator implements the FieldValidator<T> interface, where T is the TypeScript output type the chain produces.
str()
str() reads the raw string value from process.env with no coercion. It returns a StringValidator<string>.
Chainable methods
Validates that the value parses as a valid URL using the
URL constructor. Throws if new URL(val) throws.Requires the string length to be at least
n characters (val.length >= n).Requires the string length to be no more than
n characters (val.length <= n).Requires the value to match the regular expression
r (r.test(val) must be true).Requires the value to be one of the strings in
values. Narrows the output type to the literal union E. When chained after .optional(), the undefined variant is preserved in the narrowed type.Allows the variable to be absent. When the runtime value is
undefined and no .default() is set, the validator returns undefined instead of throwing.Sets a fallback value used when the variable is absent from
runtimeEnv. The output type stays string (never | undefined).Type effects
| Chain | Output type |
|---|---|
str() | string |
str().optional() | string | undefined |
str().default('x') | string |
str().enum(['a', 'b']) | 'a' | 'b' |
str().optional().enum(['a', 'b']) | 'a' | 'b' | undefined |
str().enum(['a', 'b']).default('a') | 'a' | 'b' |
str() always reads the raw string as-is. No type coercion is applied. Use num() or bool() when you need coercion.num()
num() coerces the raw string to a number using Number(rawValue). If the result is NaN, validation fails immediately and no chained rules run. It returns a NumberValidator<number>.
Chainable methods
Shorthand for
.int().min(1).max(65535). Requires the value to be an integer in the valid TCP port range.Requires the coerced number to be an integer (
Number.isInteger(val) must be true). Rejects values like "3.14".Requires the coerced number to be greater than or equal to
n.Requires the coerced number to be less than or equal to
n.Allows the variable to be absent. Returns
undefined when the runtime value is undefined and no .default() is set.Sets a numeric fallback used when the variable is absent. The output type stays
number.num() uses Number() for coercion, not parseInt. The string "3.14" becomes 3.14. Chain .int() to reject non-integers.bool()
bool() coerces the raw string to a boolean using a case-insensitive lookup against a fixed set of accepted strings. It returns a BooleanValidator<boolean>.
Accepted values
| Result | Accepted strings |
|---|---|
true | "true", "1", "yes", "on" |
false | "false", "0", "no", "off" |
"TRUE", "True", and "true" are all accepted. Any string outside these eight values causes validation to fail.
Chainable methods
Allows the variable to be absent. Returns
undefined when the runtime value is undefined and no .default() is set.Sets a boolean fallback used when the variable is absent. The output type stays
boolean.Shorthands
Two factory functions wrap common validator chains so you do not have to repeat them:Equivalent to
str().url(). Use when a variable must be a valid URL and no additional string constraints are needed.Equivalent to
num().port(), which expands to num().int().min(1).max(65535). Use for any variable that holds a TCP port number.
