Giter Site home page Giter Site logo

joi-env-parse's Introduction

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' ]
// }

joi-env-parse's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.