Quickly verify that incoming variables from process.env aren't missing.
to get up and running quickly with a verified config file, you can replace the above with something like this:
You may have values that aren't present on your env
object, but that you would like to live in your config object, this can be achieved by using the insert()
function.
const { verify, insert } = require('env-verifier')
module.exports = verify({
appName: insert('my_app')
...
}).config
{
appName: 'my_app'
...
}
As of env-verifier version 1.2.0
, the obfuscation of env secrets is supported.
by wrapping the env key of the secret in the secret
function exported by env-verifier
, the secret will be retrieved and wrapped in a Secret
object (see function specification above).
Note: support for transforming or inserting secrets is not supported at this time.
To retrieve the secret, the reveal
function can be called.
What secret obfuscation will do:
- protect secrets from casual logging of the produced config object
JSON.stringify
of the config object will replace all secrets with the string '[secret]'
What secret obfuscation will not do:
- prevent the actually logging of the revealed secret
- mutate the actual string returned from the
env
object
const { verify, secret } = require('env-verifier')
const env = {
PASSWORD: 'superSecretPassword'
}
const { config } = verify({
password: secret('PASSWORD')
...
}, env)
module.exports = config
{
password: {
reveal(): string
}
...
}
config.password.reveal()
'superSecretPassword'
console.log(config)
JSON.stringify(config)
Error reports are generated when an env
variable is missing. An env
variable is considered missing under the following circumstances:
undefined
is returned from the env
object.- an empty string,
''
, is returned from the env
object.
verify
will always return the errors array, but it will be an empty array if there are no env
misses.
strictVerify
will not throw an error on the first encountered missing env
value. Instead it will continue in order to report all missing env
variables.
Since process.env only returns strings, sometimes its necessary to transform those strings into something else (IE: transform the string "true"
to a boolean true
)
This can be done by passing in an array (TransformTuple) containing the env
variable name, and the function that you would like to use to transform the env
variable value like so:
const config = {
useNewFeature: ['USE_NEW_FEATURE', trueOrFalse => trueOrFalse === 'true'],
...
}
verify(config)
Transformation functions will not be run if its corresponding env value is missing.
env-verifier
tries to give typescript typings for the config object that it returns, but needs a little help to get the correct types
If you are using TypeScript, you can do the following:
const config: {
a: 'A',
b: insert([1, 2])
c: {
d: ['A', (envValue) => ([envValue])]
}
}
const verifiedConfig = strictVerify(config)
const verifiedConfig = strictVerify<typeof config>(config)
const config = {
a: 'A',
b: insert([1, 2])
c: {
d: ['A', (envValue) => ([envValue])] as TransformTuple<string>
}
}
const verifiedConfig = strictVerify<typeof config>(config)
Prerequisites
This package is written in TypeScript and is built/distributed for environments that support the majority of the es2016 specification.
This package also works best with projects that have centralized config files, IE: You map your .env
variables to a config
object in a file, and import
/require
that config object wherever you need .env
values.
Other than that, just install the package and get going!
One of these:
npm install env-verifier
And one of these:
const { verify, strictVerify } = require('env-verifier')
And you're all set.
Testing
After you've ran npm install
, just run npm test
.
We use jest as our testing framework.
Contributing