envalid
- Version 8.0.0
- Published
- 81.5 kB
- 1 dependency
- MIT license
Install
npm i envalid
yarn add envalid
pnpm add envalid
Overview
Validation for your environment variables
Index
Variables
Functions
Classes
Interfaces
Type Aliases
Variables
variable bool
const bool: ExactValidator<boolean>;
variable email
const email: BaseValidator<string>;
variable host
const host: BaseValidator<string>;
variable json
const json: StructuredValidator;
Unless passing a default property, it's recommended that you provide an explicit type parameter for json validation if you're using TypeScript. Otherwise the output will be typed as
any
. For example:cleanEnv({MY_VAR: json<{ foo: number }>(),})
variable num
const num: BaseValidator<number>;
variable port
const port: BaseValidator<number>;
variable str
const str: BaseValidator<string>;
variable url
const url: BaseValidator<string>;
Functions
function accessorMiddleware
accessorMiddleware: <T>(envObj: T, rawEnv: unknown) => T & CleanedEnvAccessors;
function applyDefaultMiddleware
applyDefaultMiddleware: <T>( cleanedEnv: T, rawEnv: unknown) => T & CleanedEnvAccessors;
function cleanEnv
cleanEnv: <S>( environment: unknown, specs: S, options?: CleanOptions<S>) => CleanedEnv<S>;
Returns a sanitized, immutable environment object. _Only_ the env vars specified in the
validators
parameter will be accessible on the returned object.Parameter environment
An object containing your env vars (eg. process.env).
Parameter specs
An object that specifies the format of required vars.
Parameter options
An object that specifies options for cleanEnv.
function customCleanEnv
customCleanEnv: <S, MW>( environment: unknown, specs: S, applyMiddleware: (cleaned: CleanedEnv<S>, rawEnv: unknown) => MW, options?: CleanOptions<S>) => Readonly<MW>;
Returns a sanitized, immutable environment object, and passes it through a custom applyMiddleware function before being frozen. Most users won't need the flexibility of custom middleware; prefer cleanEnv() unless you're sure you need it
Parameter environment
An object containing your env vars (eg. process.env).
Parameter specs
An object that specifies the format of required vars.
Parameter applyMiddleware
A function that applies transformations to the cleaned env object
Parameter options
An object that specifies options for cleanEnv.
function defaultReporter
defaultReporter: <T = any>( { errors }: ReporterOptions<T>, { onError, logger }?: ExtraOptions<T>) => void;
function envalidErrorFormatter
envalidErrorFormatter: <T = any>( errors: Partial<Record<keyof T, Error>>, logger?: Logger) => void;
function makeExactValidator
makeExactValidator: <T>(parseFn: (input: string) => T) => ExactValidator<T>;
Creates a validator which output type is exactly T:
const int = makeExactValidator<number>((input: string) => {// Implementation details})const MAX_RETRIES = int({ choices: [1, 2, 3, 4] })// Output type 'number'Parameter parseFn
A function to parse and validate input.
Returns
A validator which output type is exactly
T
function makeValidator
makeValidator: <BaseT>( parseFn: (input: string) => BaseT) => BaseValidator<BaseT>;
Creates a validator which can output subtypes of
BaseT
. E.g.:const int = makeValidator<number>((input: string) => {// Implementation details})const MAX_RETRIES = int({ choices: [1, 2, 3, 4] })// Narrows down output type to 1 | 2 | 3 | 4Parameter parseFn
A function to parse and validate input.
Returns
A validator which output type is narrowed-down to a subtype of
BaseT
function strictProxyMiddleware
strictProxyMiddleware: <T extends object>( envObj: T, rawEnv: unknown, options?: StrictProxyMiddlewareOptions) => T;
function testOnly
testOnly: <T>(defaultValueForTests: T) => T;
Utility function for providing default values only when NODE_ENV=test
For more context, see https://github.com/af/envalid/issues/32
Classes
class EnvError
class EnvError extends TypeError {}
constructor
constructor(message?: string);
class EnvMissingError
class EnvMissingError extends ReferenceError {}
constructor
constructor(message?: string);
Interfaces
interface BaseValidator
interface BaseValidator<BaseT> {}
call signature
<T extends BaseT>(spec: OptionalSpec<T>): OptionalValidatorSpec<T>;
call signature
(spec: ChoicelessRequiredSpec<BaseT>): RequiredValidatorSpec<BaseT>;
call signature
<T extends BaseT>(spec?: RequiredSpec<T>): RequiredValidatorSpec<T>;
interface CleanedEnvAccessors
interface CleanedEnvAccessors {}
property isDev
readonly isDev: boolean;
property isDevelopment
readonly isDevelopment: boolean;
true if NODE_ENV === 'development'
property isProd
readonly isProd: boolean;
property isProduction
readonly isProduction: boolean;
true if NODE_ENV === 'production'
property isTest
readonly isTest: boolean;
true if NODE_ENV === 'test'
interface CleanOptions
interface CleanOptions<T> {}
property reporter
reporter?: ((opts: ReporterOptions<T>) => void) | null;
Pass in a function to override the default error handling and console output. See ./reporter.ts for the default implementation.
interface ExactValidator
interface ExactValidator<T> {}
call signature
(spec: OptionalSpec<T>): OptionalValidatorSpec<T>;
call signature
(spec?: RequiredSpec<T>): RequiredValidatorSpec<T>;
interface ReporterOptions
interface ReporterOptions<T> {}
interface Spec
interface Spec<T> {}
property choices
choices?: ReadonlyArray<T>;
An Array that lists the admissable parsed values for the env var.
property default
default?: NonNullable<T> | undefined;
A fallback value, which will be used if the env var wasn't specified. Providing a default effectively makes the env var optional.
property desc
desc?: string;
A string that describes the env var.
property devDefault
devDefault?: NonNullable<T> | undefined;
A fallback value to use only when NODE_ENV is not 'production'. This is handy for env vars that are required for production environments, but optional for development and testing.
property docs
docs?: string;
A url that leads to more detailed documentation about the env var.
property example
example?: string;
An example value for the env var.
interface StrictProxyMiddlewareOptions
interface StrictProxyMiddlewareOptions {}
property extraInspectables
extraInspectables?: string[];
A list of extra inspectable properties to add to the middleware.
This is useful if you want to add support for framework-specific values.
interface StructuredValidator
interface StructuredValidator {}
call signature
(): RequiredValidatorSpec<any>;
call signature
<T>(): RequiredValidatorSpec<T>;
call signature
(spec: RequiredTypelessSpec): RequiredValidatorSpec<any>;
call signature
(spec: OptionalTypelessSpec): OptionalValidatorSpec<any>;
call signature
<T>(spec: ChoicelessOptionalSpec<T>): OptionalValidatorSpec<T>;
call signature
<T>(spec: ChoicelessRequiredSpec<T>): RequiredValidatorSpec<T>;
Type Aliases
type CleanedEnv
type CleanedEnv<S> = S extends Record<string, ValidatorSpec<unknown>> ? Readonly< { [K in keyof S]: S[K] extends OptionalValidatorSpec<infer U> ? U | undefined : S[K] extends RequiredValidatorSpec<infer U> ? U : never; } & CleanedEnvAccessors > : never;
type OptionalValidatorSpec
type OptionalValidatorSpec<T> = OptionalSpec<T> & WithParser<T>;
type RequiredValidatorSpec
type RequiredValidatorSpec<T> = RequiredSpec<T> & WithParser<T>;
type SpecsOutput
type SpecsOutput<S> = { [K in keyof S]: unknown;};
type ValidatorSpec
type ValidatorSpec<T> = RequiredValidatorSpec<T> | OptionalValidatorSpec<T>;
Package Files (8)
Dependencies (1)
Dev Dependencies (8)
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/envalid
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/envalid)
- HTML<a href="https://www.jsdocs.io/package/envalid"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 3520 ms. - Missing or incorrect documentation? Open an issue for this package.