@@ -18,6 +18,12 @@ optionally provide defaults (which can be matched against `NODE_ENV` values like
1818` production ` or ` development ` ), as well as help strings that will be included in
1919the error thrown when an env var is missing.
2020
21+ ## Features
22+
23+ - No dependencies
24+ - Fully type-safe
25+ - Compatible with serverless environments (import ` znv/compat ` instead of ` znv ` )
26+
2127## Status
2228
2329Unstable: znv has not yet hit v1.0.0, and per semver there may be breaking
@@ -31,7 +37,7 @@ about final API design are welcome.
3137- [ Quickstart] ( #quickstart )
3238- [ Motivation] ( #motivation )
3339- [ Usage] ( #usage )
34- - [ ` parseEnv ` ] ( #parseenvenvironment-schemas )
40+ - [ ` parseEnv ` ] ( #parseenvenvironment-schemas-reporterOrFormatters )
3541 - [ Extra schemas] ( #extra-schemas )
3642- [ Coercion rules] ( #coercion-rules )
3743- [ Comparison to other libraries] ( #comparison-to-other-libraries )
@@ -43,6 +49,8 @@ about final API design are welcome.
4349``` bash
4450npm i znv zod
4551# or
52+ pnpm add znv zod
53+ # or
4654yarn add znv zod
4755```
4856
@@ -200,7 +208,7 @@ environments is not straightforward.
200208
201209## Usage
202210
203- ### ` parseEnv(environment, schemas) `
211+ ### ` parseEnv(environment, schemas, reporterOrFormatters? ) `
204212
205213Parse the given ` environment ` using the given ` schemas ` . Returns a read-only
206214object that maps the keys of the ` schemas ` object to their respective parsed
@@ -209,6 +217,13 @@ values.
209217Throws if any schema fails to parse its respective env var. The error aggregates
210218all parsing failures for the schemas.
211219
220+ Optionally, you can pass a custom error reporter as the third parameter to
221+ ` parseEnv ` to customize how errors are displayed. The reporter is a function
222+ that receives error details and returns a ` string ` . Alternately, you can pass an
223+ object of _ token formatters_ as the third parameter to ` parseEnv ` ; this can be
224+ useful if you want to retain the default error reporting format but want to
225+ customize some aspects of it (for example, by redacting secrets).
226+
212227#### ` environment: Record<string, string | undefined> `
213228
214229You usually want to pass in ` process.env ` as the first argument.
@@ -308,6 +323,66 @@ pass a `DetailedSpec` object that has the following fields:
308323 ` NODE_ENV: z.enum(["production", "development", "test", "ci"]) ` to enforce
309324 that ` NODE_ENV ` is always defined and is one of those four expected values.
310325
326+ #### ` reporterOrFormatters?: Reporter | TokenFormatters `
327+
328+ An optional error reporter or object of error token formatters, for customizing
329+ the displayed output when a validation error occurs.
330+
331+ - ` Reporter: (errors: ErrorWithContext[], schemas: Schemas) => string `
332+
333+ A reporter is a function that takes a list of errors and the schemas you
334+ passed to ` parseEnv ` and returns a ` string ` . Each error has the following
335+ format:
336+
337+ ``` ts
338+ {
339+ /** The env var name. */
340+ key : string ;
341+ /** The actual value present in `process.env[key]`, or undefined. */
342+ receivedValue : unknown ;
343+ /** `ZodError` if Zod parsing failed, or `Error` if a preprocessor threw. */
344+ error : unknown ;
345+ /** If a default was provided, whether the default value was used. */
346+ defaultUsed : boolean ;
347+ /** If a default was provided, the given default value. */
348+ defaultValue : unknown ;
349+ }
350+ ```
351+
352+ - ` TokenFormatters `
353+
354+ An object with the following structure:
355+
356+ ``` ts
357+ {
358+ /** Formatter for the env var name. */
359+ formatVarName ?: (key : string ) => string ;
360+
361+ /** For parsed objects with errors, formatter for object keys. */
362+ formatObjKey ?: (key : string ) => string ;
363+
364+ /** Formatter for the actual value we received for the env var. */
365+ formatReceivedValue ?: (val : unknown ) => string ;
366+
367+ /** Formatter for the default value provided for the schema. */
368+ formatDefaultValue ?: (val : unknown ) => string ;
369+
370+ /** Formatter for the error summary header. */
371+ formatHeader ?: (header : string ) => string ;
372+ }
373+ ```
374+
375+ For example, if you want to redact value names, you can invoke ` parseEnv ` like
376+ this:
377+
378+ ``` ts
379+ export const { SOME_VAL } = parseEnv (
380+ process .env ,
381+ { SOME_VAL: z .number ().nonnegative () },
382+ { formatReceivedValue : () => " <redacted>" },
383+ );
384+ ```
385+
311386### Extra schemas
312387
313388znv exports a very small number of extra schemas for common env var types.
0 commit comments