Skip to content

Commit

Permalink
Merge 413af73 into 8ab5c76
Browse files Browse the repository at this point in the history
  • Loading branch information
HEYGUL committed Apr 17, 2018
2 parents 8ab5c76 + 413af73 commit 0c6e33e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,16 @@ const help = envier.helpString()
Reference
---------

#### `[new] Envie(description[, values=process.env])`
#### `[new] Envie(description[, values=process.env], options={ noDefaults: false })`

Creates a new `envie` instance which parses the keys of the object `values` using the `description`.
`description` must be an object whose keys contain a [Joi](https://www.npmjs.com/package/joi) validator.
By default, `process.env` is parsed. The `new` keyword is optional.

You can specifiy `noDefaults` to true to get `undefined` instead of default value for non set key.
This is specially useful if you want to use default values on local environment but want to ensure all required keys
have a defined values when application is deployed.

#### `envie.get(key)`

Reads the parsed value for `key` if present and valid. Throws if the value is not valid.
Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ module.exports = Envie
Envie.Envie = Envie
Envie.Joi = Joi

function Envie (description, values) {
if (!(this instanceof Envie)) return new Envie(description, values)
function Envie (description, values, options) {
if (!(this instanceof Envie)) return new Envie(description, values, options)
if (!values) values = process.env
if (!options) options = { noDefaults: false }

this.get = function (key) {
const { error, value } = validate(key)
Expand Down Expand Up @@ -40,7 +41,7 @@ function Envie (description, values) {
}

function validate (key) {
return getValidator(key).validate(values[key])
return getValidator(key).validate(values[key], { noDefaults: options.noDefaults })
}

function getValidator (key) {
Expand Down
8 changes: 8 additions & 0 deletions test/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ describe('new Envie({descriptions...}, {...values})', () => {
it('returns the default value', () => {
expect(envie.get('with_default')).to.equal('hello world')
})

describe('but noDefaults option is true', () => {
const envie = Envie(description, {}, { noDefaults: true })
it('returns undefined', () => {
expect(envie.get('with_default')).to.equal(undefined)
})
})
})
})

Expand All @@ -45,6 +52,7 @@ describe('new Envie({descriptions...}, {...values})', () => {
expect(() => envie.get('invalid')).to.throw(/invalid is not valid/i)
})
})

})
})

Expand Down

0 comments on commit 0c6e33e

Please sign in to comment.