+3
-3
@@ -62,2 +62,5 @@ "use strict"; | ||
| cleanedEnv[k] = spec.devDefault; | ||
| if (isTestOnlySymbol(spec.devDefault) && rawNodeEnv != 'test') { | ||
| throw new errors_1.EnvMissingError(formatSpecDescription(spec)); | ||
| } | ||
| continue; | ||
@@ -71,5 +74,2 @@ } | ||
| try { | ||
| if (isTestOnlySymbol(rawValue)) { | ||
| throw new errors_1.EnvMissingError(formatSpecDescription(spec)); | ||
| } | ||
| if (rawValue === undefined) { | ||
@@ -76,0 +76,0 @@ cleanedEnv[k] = undefined; |
+1
-1
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":";;;AAAA,mCAAoD;AAEpD,uCAA4C;AAE/B,QAAA,cAAc,GAAG,MAAM,CAAC,qBAAqB,CAAC,CAAA;AAE3D;;;;;GAKG;AACH,SAAS,WAAW,CAAI,EAQvB;QAPC,IAAI,UAAA,EACJ,IAAI,UAAA,EACJ,QAAQ,cAAA;IAMR,IAAI,OAAO,IAAI,CAAC,MAAM,KAAK,UAAU,EAAE;QACrC,MAAM,IAAI,iBAAQ,CAAC,6BAAqB,IAAI,OAAG,CAAC,CAAA;KACjD;IACD,IAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,QAAkB,CAAC,CAAA;IAE7C,IAAI,IAAI,CAAC,OAAO,EAAE;QAChB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE;YAChC,MAAM,IAAI,SAAS,CAAC,sDAA4C,IAAI,QAAI,CAAC,CAAA;SAC1E;aAAM,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE;YACxC,MAAM,IAAI,iBAAQ,CAAC,kBAAU,KAAK,gCAAqB,IAAI,CAAC,OAAO,MAAG,CAAC,CAAA;SACxE;KACF;IACD,IAAI,KAAK,IAAI,IAAI;QAAE,MAAM,IAAI,iBAAQ,CA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IAAI,MAAM,EAAE;oBAC7D,MAAM,IAAI,wBAAe,CAAC,qBAAqB,CAAC,IAAI,CAAC,CAAC,CAAA;iBACvD;gBAED,SAAQ;aACT;YACD,IAAI,SAAS,IAAI,IAAI,EAAE;gBACrB,UAAU,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,SAAO,CAAA,CAAA;gBAC5B,SAAQ;aACT;SACF;QAED,IAAI;YACF,IAAI,QAAQ,KAAK,SAAS,EAAE;gBAC1B,UAAU,CAAC,CAAC,CAAC,GAAG,SAAS,CAAA;gBACzB,MAAM,IAAI,wBAAe,CAAC,qBAAqB,CAAC,IAAI,CAAC,CAAC,CAAA;aACvD;iBAAM;gBACL,UAAU,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,EAAE,IAAI,EAAE,CAAW,EAAE,IAAI,MAAA,EAAE,QAAQ,UAAA,EAAE,CAAC,CAAA;aACnE;SACF;QAAC,OAAO,GAAG,EAAE;YACZ,IAAI,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAQ,MAAK,IAAI;gBAAE,MAAM,GAAG,CAAA;YACzC,IAAI,GAAG,YAAY,KAAK;gBAAE,MAAM,CAAC,CAAC,CAAC,GAAG,GAAG,CAAA;SAC1C;KACF;IAED,IAAM,QAAQ,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAQ,KAAI,0BAAe,CAAA;IACrD,QAAQ,CAAC,EAAE,MAAM,QAAA,EAAE,GAAG,EAAE,UAAU,EAAE,CAAC,CAAA;IACrC,OAAO,UAAU,CAAA;AACnB,CAAC;AApDD,0CAoDC"} |
+24
-16
@@ -28,23 +28,31 @@ export interface Spec<T> { | ||
| } | ||
| type OptionalSpec<T> = Omit<Spec<T>, 'default'> & { | ||
| type OptionalAttrs<T> = { | ||
| default: undefined; | ||
| }; | ||
| type OptionalTypelessSpec = Omit<OptionalSpec<unknown>, 'choices'>; | ||
| type RequiredSpec<T> = (Spec<T> & { | ||
| } | { | ||
| devDefault: undefined; | ||
| } | { | ||
| default: undefined; | ||
| devDefault: undefined; | ||
| } | { | ||
| default: NonNullable<T>; | ||
| }) | Omit<Spec<T>, 'default'>; | ||
| type RequiredTypelessSpec = Omit<Spec<unknown>, 'choices' | 'default'> & { | ||
| devDefault?: undefined; | ||
| }; | ||
| type ChoicelessOptionalSpec<T> = Omit<Spec<T>, 'default' | 'choices'> & { | ||
| devDefault: undefined; | ||
| } | { | ||
| default: undefined; | ||
| devDefault: NonNullable<T>; | ||
| }; | ||
| type ChoicelessRequiredSpec<T> = (Omit<Spec<T>, 'choices'> & { | ||
| type RequiredAttrs<T> = { | ||
| default: NonNullable<T>; | ||
| }) | Omit<Spec<T>, 'default' | 'choices'>; | ||
| type ChoicelessRequiredSpecWithType<T> = ChoicelessRequiredSpec<T> & ({ | ||
| default: NonNullable<T>; | ||
| } | { | ||
| devDefault: NonNullable<T>; | ||
| }); | ||
| } | { | ||
| devDefault: NonNullable<T>; | ||
| default: NonNullable<T>; | ||
| } | {}; | ||
| type DefaultKeys = 'default' | 'devDefault'; | ||
| type OptionalSpec<T> = Spec<T> & OptionalAttrs<T>; | ||
| type OptionalTypelessSpec = Omit<OptionalSpec<unknown>, 'choices'>; | ||
| type RequiredSpec<T> = Spec<T> & RequiredAttrs<T>; | ||
| type RequiredTypelessSpec = Omit<RequiredSpec<unknown>, 'choices' | DefaultKeys>; | ||
| type ChoicelessOptionalSpec<T> = Omit<Spec<T>, 'choices' | DefaultKeys> & OptionalAttrs<T>; | ||
| type ChoicelessRequiredSpec<T> = Omit<Spec<T>, 'choices' | DefaultKeys> & RequiredAttrs<T>; | ||
| type WithParser<T> = { | ||
@@ -57,8 +65,8 @@ _parse: (input: string) => T; | ||
| export interface ExactValidator<T> { | ||
| (spec: OptionalSpec<T>): OptionalValidatorSpec<T>; | ||
| (spec?: RequiredSpec<T>): RequiredValidatorSpec<T>; | ||
| (spec: OptionalSpec<T>): OptionalValidatorSpec<T>; | ||
| } | ||
| export interface BaseValidator<BaseT> { | ||
| <T extends BaseT>(spec: OptionalSpec<T>): OptionalValidatorSpec<T>; | ||
| (spec: ChoicelessRequiredSpecWithType<BaseT>): RequiredValidatorSpec<BaseT>; | ||
| (spec: ChoicelessRequiredSpec<BaseT>): RequiredValidatorSpec<BaseT>; | ||
| <T extends BaseT>(spec?: RequiredSpec<T>): RequiredValidatorSpec<T>; | ||
@@ -65,0 +73,0 @@ } |
+1
-1
| { | ||
| "name": "envalid", | ||
| "version": "8.0.0-alpha.2", | ||
| "version": "8.0.0-beta.1", | ||
| "description": "Validation for your environment variables", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
+62
-89
@@ -11,3 +11,3 @@ <p align="center"> | ||
| Envalid is a small library for validating and accessing<br /> | ||
| environment variables in Node.js (v8.12 or later) programs | ||
| environment variables in Node.js programs | ||
| </strong> | ||
@@ -23,37 +23,15 @@ </p> | ||
| Envalid is a small library for validating and accessing environment variables in | ||
| Node.js (v8.12 or later) programs, aiming to: | ||
| Node.js programs, aiming to: | ||
| * ensure that your program only runs when all of its environment dependencies are met | ||
| * give you executable documentation about the environment your program expects to run in | ||
| * give you an immutable API for your environment variables, so they don't change | ||
| - ensure that your program only runs when all of its environment dependencies are met | ||
| - give you executable documentation about the environment your program expects to run in | ||
| - give you an immutable API for your environment variables, so they don't change | ||
| from under you while the program is running | ||
| ## Changes in v7.x | ||
| ## Why envalid? | ||
| Version 7 is a major update, with several breaking changes. Please review the breaking changes | ||
| below before upgrading: | ||
| - Type-safe: written completely in TypeScript, with great support for inference | ||
| - Light: no dependencies besides [tslib](https://github.com/Microsoft/tslib) | ||
| - Modular: customize behavior with custom validators, middleware, and reporters | ||
| * Rewritten in TypeScript | ||
| * Removed _all_ runtime dependencies except for [tslib](https://github.com/Microsoft/tslib) | ||
| * The mode-currently-known-as-`strict` is removed, and its behavior is enabled by default. This means: | ||
| * The env object will *only* contain the env vars that were specified by your `validators`. | ||
| * Any attempt to access an invalid/missing property on the env object will cause a thrown error. | ||
| * Any attempt to mutate the cleaned env object will cause a thrown error. | ||
| You can still opt-out of strict mode by disabling the `strictProxyMiddleware`, but it's not | ||
| recommended (see "Custom Middleware", below). | ||
| * The `dotenv` package is no longer shipped as part of this library. You can easily use it directly | ||
| by installing it and running `require('dotenv').config()` before you invoke envalid's `cleanEnv()` | ||
| * The `transformer` validator option is gone, replaced by the ability to add custom middleware | ||
| * The `host` and `ip` validators are now slightly less exhaustive. If you need these to be airtight, use | ||
| your own custom validator instead | ||
| * When you try to access an invalid property on the cleaned env object, the error will no longer | ||
| suggest an env variable that you may have intended. You can re-implement the old behavior with a custom | ||
| middleware if you wish | ||
| * `NODE_ENV` support is now less opinionated, and an error is no longer thrown if a value other | ||
| than `production`/`development`/`test` is passed in. You can provide your own validator for `NODE_ENV` | ||
| to get exactly the behavior you want. The `isDev`, `isProduction`, etc properties still work as | ||
| before, and are implemented as middleware so you can override their behavior as needed. | ||
| * `devDefault` values are no longer used if `NODE_ENV` was not set in the environment (a case where | ||
| Envalid otherwise assumes `'production'` mode). Fixes #65 | ||
| ## API | ||
@@ -66,7 +44,7 @@ | ||
| * `environment` - An object containing your env vars (eg. `process.env`) | ||
| * `validators` - An object that specifies the format of required vars. | ||
| * `options` - An (optional) object, which supports the following key: | ||
| * `reporter` - Pass in a function to override the default error handling and | ||
| console output. See `src/reporter.ts` for the default implementation. | ||
| - `environment` - An object containing your env vars (eg. `process.env`) | ||
| - `validators` - An object that specifies the format of required vars. | ||
| - `options` - An (optional) object, which supports the following key: | ||
| - `reporter` - Pass in a function to override the default error handling and | ||
| console output. See `src/reporter.ts` for the default implementation. | ||
@@ -80,18 +58,17 @@ By default, `cleanEnv()` will log an error message and exit (in Node) or throw (in browser) if any required | ||
| const env = cleanEnv(process.env, { | ||
| API_KEY: str(), | ||
| ADMIN_EMAIL: email({ default: 'admin@example.com' }), | ||
| EMAIL_CONFIG_JSON: json({ desc: 'Additional email parameters' }), | ||
| NODE_ENV: str({ choices: ['development', 'test', 'production', 'staging']}), | ||
| API_KEY: str(), | ||
| ADMIN_EMAIL: email({ default: 'admin@example.com' }), | ||
| EMAIL_CONFIG_JSON: json({ desc: 'Additional email parameters' }), | ||
| NODE_ENV: str({ choices: ['development', 'test', 'production', 'staging'] }), | ||
| }) | ||
| // Read an environment variable, which is validated and cleaned during | ||
| // and/or filtering that you specified with cleanEnv(). | ||
| env.ADMIN_EMAIL // -> 'admin@example.com' | ||
| env.ADMIN_EMAIL // -> 'admin@example.com' | ||
| // Envalid checks for NODE_ENV automatically, and provides the following | ||
| // shortcut (boolean) properties for checking its value: | ||
| env.isProduction // true if NODE_ENV === 'production' | ||
| env.isTest // true if NODE_ENV === 'test' | ||
| env.isDev // true if NODE_ENV === 'development' | ||
| env.isProduction // true if NODE_ENV === 'production' | ||
| env.isTest // true if NODE_ENV === 'test' | ||
| env.isDev // true if NODE_ENV === 'development' | ||
| ``` | ||
@@ -114,27 +91,26 @@ | ||
| * `str()` - Passes string values through, will ensure an value is present unless a | ||
| `default` value is given. Note that an empty string is considered a valid value - | ||
| if this is undesirable you can easily create your own validator (see below) | ||
| * `bool()` - Parses env var strings `"1", "0", "true", "false", "t", "f"` into booleans | ||
| * `num()` - Parses an env var (eg. `"42", "0.23", "1e5"`) into a Number | ||
| * `email()` - Ensures an env var is an email address | ||
| * `host()` - Ensures an env var is either a domain name or an ip address (v4 or v6) | ||
| * `port()` - Ensures an env var is a TCP port (1-65535) | ||
| * `url()` - Ensures an env var is a url with a protocol and hostname | ||
| * `json()` - Parses an env var with `JSON.parse` | ||
| - `str()` - Passes string values through, will ensure an value is present unless a | ||
| `default` value is given. Note that an empty string is considered a valid value - | ||
| if this is undesirable you can easily create your own validator (see below) | ||
| - `bool()` - Parses env var strings `"1", "0", "true", "false", "t", "f"` into booleans | ||
| - `num()` - Parses an env var (eg. `"42", "0.23", "1e5"`) into a Number | ||
| - `email()` - Ensures an env var is an email address | ||
| - `host()` - Ensures an env var is either a domain name or an ip address (v4 or v6) | ||
| - `port()` - Ensures an env var is a TCP port (1-65535) | ||
| - `url()` - Ensures an env var is a url with a protocol and hostname | ||
| - `json()` - Parses an env var with `JSON.parse` | ||
| Each validation function accepts an (optional) object with the following attributes: | ||
| * `choices` - An Array that lists the admissable parsed values for the env var. | ||
| * `default` - A fallback value, which will be present in the output if the env var wasn't specified. | ||
| Providing a default effectively makes the env var optional. Note that `default` | ||
| values are not passed through validation logic, they are default *output* values. | ||
| * `devDefault` - A fallback value to use *only* when `NODE_ENV` is explicitly set and _not_ `'production'`. | ||
| This is handy for env vars that are required for production environments, but optional | ||
| for development and testing. | ||
| * `desc` - A string that describes the env var. | ||
| * `example` - An example value for the env var. | ||
| * `docs` - A url that leads to more detailed documentation about the env var. | ||
| - `choices` - An Array that lists the admissable parsed values for the env var. | ||
| - `default` - A fallback value, which will be present in the output if the env var wasn't specified. | ||
| Providing a default effectively makes the env var optional. Note that `default` | ||
| values are not passed through validation logic, they are default _output_ values. | ||
| - `devDefault` - A fallback value to use _only_ when `NODE_ENV` is explicitly set and _not_ `'production'`. | ||
| This is handy for env vars that are required for production environments, but optional | ||
| for development and testing. | ||
| - `desc` - A string that describes the env var. | ||
| - `example` - An example value for the env var. | ||
| - `docs` - A url that leads to more detailed documentation about the env var. | ||
| ## Custom validators | ||
@@ -150,11 +126,12 @@ | ||
| import { makeValidator, cleanEnv } from 'envalid' | ||
| const twochars = makeValidator(x => { | ||
| if (/^[A-Za-z]{2}$/.test(x)) return x.toUpperCase() | ||
| else throw new Error('Expected two letters') | ||
| const twochars = makeValidator((x) => { | ||
| if (/^[A-Za-z]{2}$/.test(x)) return x.toUpperCase() | ||
| else throw new Error('Expected two letters') | ||
| }) | ||
| const env = cleanEnv(process.env, { | ||
| INITIALS: twochars() | ||
| }); | ||
| INITIALS: twochars(), | ||
| }) | ||
| ``` | ||
| ### TypeScript users | ||
@@ -206,5 +183,5 @@ | ||
| const env = cleanEnv(process.env, myValidators, { | ||
| reporter: ({ errors, env }) => { | ||
| emailSiteAdmins('Invalid env vars: ' + Object.keys(errors)) | ||
| } | ||
| reporter: ({ errors, env }) => { | ||
| emailSiteAdmins('Invalid env vars: ' + Object.keys(errors)) | ||
| }, | ||
| }) | ||
@@ -231,3 +208,2 @@ ``` | ||
| ## Custom Middleware (advanced) | ||
@@ -244,8 +220,7 @@ | ||
| * `applyMiddleware` - A functions that can modify the env object after it's | ||
| validated and cleaned. Envalid ships (and exports) its own default | ||
| middleware (see src/middleware.ts), which you can mix and match with your own | ||
| custom logic to get the behavior you desire. | ||
| - `applyMiddleware` - A functions that can modify the env object after it's | ||
| validated and cleaned. Envalid ships (and exports) its own default | ||
| middleware (see src/middleware.ts), which you can mix and match with your own | ||
| custom logic to get the behavior you desire. | ||
| ## Utils | ||
@@ -260,3 +235,3 @@ | ||
| const env = cleanEnv(process.env, { | ||
| SOME_VAR: envalid.str({devDefault: testOnly('myTestValue')}) | ||
| SOME_VAR: envalid.str({ devDefault: testOnly('myTestValue') }), | ||
| }) | ||
@@ -273,20 +248,18 @@ ``` | ||
| ## Related projects | ||
| * [dotenv](https://www.npmjs.com/package/dotenv) is a very handy tool for loading env vars from | ||
| - [dotenv](https://www.npmjs.com/package/dotenv) is a very handy tool for loading env vars from | ||
| `.env` files. It was previously used as a dependency of Envalid's. To use them together, simply | ||
| call `require('dotenv').config()` before you pass `process.env` to your `envalid.cleanEnv()`. | ||
| * [react-native-config](https://www.npmjs.com/package/react-native-config) can be useful for React Native projects for reading env vars from a `.env` file | ||
| - [react-native-config](https://www.npmjs.com/package/react-native-config) can be useful for React Native projects for reading env vars from a `.env` file | ||
| * [fastify-envalid](https://github.com/alemagio/fastify-envalid) is a wrapper for using Envalid within [Fastify](https://www.fastify.io/) | ||
| - [fastify-envalid](https://github.com/alemagio/fastify-envalid) is a wrapper for using Envalid within [Fastify](https://www.fastify.io/) | ||
| * [nestjs-envalid](https://github.com/cobraz/nestjs-envalid) is a wrapper for using Envalid with [NestJS](https://nestjs.com/) | ||
| - [nestjs-envalid](https://github.com/cobraz/nestjs-envalid) is a wrapper for using Envalid with [NestJS](https://nestjs.com/) | ||
| * [nuxt-envalid](https://github.com/manuelhenke/nuxt-envalid) is a wrapper for using Envalid with [NuxtJS](https://nuxtjs.org/) | ||
| - [nuxt-envalid](https://github.com/manuelhenke/nuxt-envalid) is a wrapper for using Envalid with [NuxtJS](https://nuxtjs.org/) | ||
| ## Motivation | ||
| http://www.12factor.net/config | ||
| http://www.12factor.net/config |
+5
-4
@@ -77,2 +77,7 @@ import { EnvError, EnvMissingError } from './errors' | ||
| cleanedEnv[k] = spec.devDefault | ||
| if (isTestOnlySymbol(spec.devDefault) && rawNodeEnv != 'test') { | ||
| throw new EnvMissingError(formatSpecDescription(spec)) | ||
| } | ||
| continue | ||
@@ -87,6 +92,2 @@ } | ||
| try { | ||
| if (isTestOnlySymbol(rawValue)) { | ||
| throw new EnvMissingError(formatSpecDescription(spec)) | ||
| } | ||
| if (rawValue === undefined) { | ||
@@ -93,0 +94,0 @@ cleanedEnv[k] = undefined |
+20
-23
@@ -29,27 +29,24 @@ export interface Spec<T> { | ||
| type OptionalSpec<T> = Omit<Spec<T>, 'default'> & { default: undefined } | ||
| type OptionalTypelessSpec = Omit<OptionalSpec<unknown>, 'choices'> | ||
| type OptionalAttrs<T> = | ||
| | { default: undefined } | ||
| | { devDefault: undefined } | ||
| | { default: undefined; devDefault: undefined } | ||
| | { default: NonNullable<T>; devDefault: undefined } | ||
| | { default: undefined; devDefault: NonNullable<T> } | ||
| type RequiredAttrs<T> = | ||
| | { default: NonNullable<T> } | ||
| | { devDefault: NonNullable<T> } | ||
| | { devDefault: NonNullable<T>; default: NonNullable<T> } | ||
| | {} | ||
| type RequiredSpec<T> = (Spec<T> & { default: NonNullable<T> }) | Omit<Spec<T>, 'default'> | ||
| type RequiredTypelessSpec = Omit<Spec<unknown>, 'choices' | 'default'> & { | ||
| devDefault?: undefined | ||
| } | ||
| type DefaultKeys = 'default' | 'devDefault' | ||
| type ChoicelessOptionalSpec<T> = Omit<Spec<T>, 'default' | 'choices'> & { | ||
| default: undefined | ||
| } | ||
| type OptionalSpec<T> = Spec<T> & OptionalAttrs<T> | ||
| type OptionalTypelessSpec = Omit<OptionalSpec<unknown>, 'choices'> | ||
| type ChoicelessRequiredSpec<T> = | ||
| | (Omit<Spec<T>, 'choices'> & { default: NonNullable<T> }) | ||
| | Omit<Spec<T>, 'default' | 'choices'> | ||
| type RequiredSpec<T> = Spec<T> & RequiredAttrs<T> | ||
| type RequiredTypelessSpec = Omit<RequiredSpec<unknown>, 'choices' | DefaultKeys> | ||
| type ChoicelessRequiredSpecWithType<T> = ChoicelessRequiredSpec<T> & | ||
| ( | ||
| | { | ||
| default: NonNullable<T> | ||
| } | ||
| | { | ||
| devDefault: NonNullable<T> | ||
| } | ||
| ) | ||
| type ChoicelessOptionalSpec<T> = Omit<Spec<T>, 'choices' | DefaultKeys> & OptionalAttrs<T> | ||
| type ChoicelessRequiredSpec<T> = Omit<Spec<T>, 'choices' | DefaultKeys> & RequiredAttrs<T> | ||
@@ -69,4 +66,4 @@ type WithParser<T> = { | ||
| export interface ExactValidator<T> { | ||
| (spec: OptionalSpec<T>): OptionalValidatorSpec<T> | ||
| (spec?: RequiredSpec<T>): RequiredValidatorSpec<T> | ||
| (spec: OptionalSpec<T>): OptionalValidatorSpec<T> | ||
| } | ||
@@ -80,3 +77,3 @@ | ||
| <T extends BaseT>(spec: OptionalSpec<T>): OptionalValidatorSpec<T> | ||
| (spec: ChoicelessRequiredSpecWithType<BaseT>): RequiredValidatorSpec<BaseT> | ||
| (spec: ChoicelessRequiredSpec<BaseT>): RequiredValidatorSpec<BaseT> | ||
| <T extends BaseT>(spec?: RequiredSpec<T>): RequiredValidatorSpec<T> | ||
@@ -83,0 +80,0 @@ } |
1360
0.37%79891
-1.91%255
-9.25%