joi-env-parse
Validates and formats process.env
using Joi schemas. Recommended using this library along dotenv.
- All object keys are camel-cased.
- Every key not defined in the schema are discarded
- Supports nested objects using
__
as separator - Throws joi
ValidationError
if parse attempt fails
Usage
yarn add joi-env-parsenpm install joi-env-parse
const parse = const config = // { // listenPort: 3000, // cookieSecret: 'mysecret' // }
Examples
Possible and default values
NODE_ENV=development
// { // port: 5000, // nodeEnv: 'development' // }
Nested properties
MONGO_URI=mongodb://localhostREDIS__HOST_ADDR=localhostREDIS__DB=7NESTED__OBJ__FOO__BAR=myvalue
// { // mongoUri: 'mongodb://localhost',// redis: { hostAddr: 'localhost', db: 7 }// nested: { obj: { foo: { bar: 'myvalue' } } }// }
Object and array values
MY_PARAMS={"a":1,"b":"foo"}IP_WHITELIST=["192.168.1.1","localhost"]
// { // myParams: { a: 1, b: 'foo' },// ipWhitelist: [ '192.168.1.1', 'localhost' ]// }