joi-env-parse

1.0.0 • Public • Published

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-parse
npm install joi-env-parse
const { parse } = require('joi-env-parse')
 
const config = parse( joi => ({
  LISTEN_PORT: joi.number().required(),
  COOKIE_SECRET: joi.string().required()
}))
 
// { 
//   listenPort: 3000, 
//   cookieSecret: 'mysecret' 
// }
 

Examples

Possible and default values

NODE_ENV=development
parse( joi => ({
  NODE_ENV: joi.string().required().valid('development', 'production'),
  PORT: joi.number().default(5000)
}))
 
// { 
//   port: 5000, 
//   nodeEnv: 'development' 
// }
 

Nested properties

MONGO_URI=mongodb://localhost
REDIS__HOST_ADDR=localhost
REDIS__DB=7
NESTED__OBJ__FOO__BAR=myvalue
parse( joi => ({
  MONGO_URI: joi.string().uri().required(),
  REDIS__HOST_ADDR: joi.string().required(),
  REDIS__DB: joi.number().required(),
  NESTED__OBJ__FOO__BAR: joi.number().required()
}))
 
// { 
//   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"]
parse( joi => ({
  MY_PARAMS: joi.object().required(),
  IP_WHITELIST: joi.array().required()
}))
 
// { 
//   myParams: { a: 1, b: 'foo' },
//   ipWhitelist: [ '192.168.1.1', 'localhost' ]
// }
 

Package Sidebar

Install

npm i joi-env-parse

Weekly Downloads

10

Version

1.0.0

License

ISC

Unpacked Size

3.85 kB

Total Files

3

Last publish

Collaborators

  • edus44