Type Coercion Hooks
Supply a coerce function to transform the raw string from process.env before type parsing and validation.
Usage
ts
import { createEnv } from "@frauschert/env-guard";
const env = createEnv({
// Parse a JSON string
CONFIG: {
type: "string",
required: true,
coerce: (raw) => JSON.parse(raw),
},
// Decode base64
SECRET: {
type: "string",
required: true,
coerce: (raw) => Buffer.from(raw, "base64").toString("utf-8"),
},
// Strip currency symbol before number parsing
PRICE: {
type: "number",
required: true,
coerce: (raw) => parseFloat(raw.replace("$", "")),
},
// Parse a JSON array instead of comma-separated
TAGS: {
type: "array",
itemType: "string",
required: true,
coerce: (raw) => JSON.parse(raw),
},
});How It Works
coercereceives the raw env string and returns the transformed value.- It runs before type parsing — the returned value replaces the normal parse result.
- Validation (
choices,validate,format) still applies to the coerced value. - For
type: "array",coercebypasses the default split-by-separator logic — you control the full parsing. - When the variable is missing,
coerceis not called anddefaultis used as normal.
Common Recipes
JSON Config
ts
CONFIG: {
type: "string",
required: true,
coerce: (raw) => JSON.parse(raw),
}
// CONFIG='{"port":3000}' → { port: 3000 }Base64 Decoding
ts
CERT: {
type: "string",
required: true,
coerce: (raw) => Buffer.from(raw, "base64").toString("utf-8"),
}Trimming Whitespace
ts
TOKEN: {
type: "string",
required: true,
coerce: (raw) => raw.trim(),
}Custom Number Parsing
ts
PRICE: {
type: "number",
required: true,
coerce: (raw) => parseFloat(raw.replace(/[^0-9.]/g, "")),
}
// PRICE="$42.50" → 42.5