Skip to content

Commit 5b3de89

Browse files
authored
feat: add isRequired argument to required function
1 parent 8ee1271 commit 5b3de89

File tree

6 files changed

+27
-6
lines changed

6 files changed

+27
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 3.5.0 (02/29/19)
2+
* Update `required()` to support boolean paramter to bypass the check
3+
14
## 3.4.2 (06/11/18)
25
* Fix README badge copy/paste error
36

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,27 @@ try {
116116
A variable is returned by calling `env.get`. It has the exposes the following
117117
functions to validate and access the underlying value.
118118

119-
#### required()
119+
#### required(isRequired = true)
120120
Ensure the variable is set on *process.env*. If the variable is not set this
121121
function will throw an `EnvVarError`. If the variable is set it returns itself
122122
so you can access the underlying variable.
123123

124-
For example:
124+
Can be bypassed by passing `false`, i.e `required(false)`
125+
126+
Full example:
125127

126128
```js
127129
const env = require('env-var')
128130

129131
// Read PORT variable and ensure it's a positive integer. If it is not a
130132
// positive integer or is not set the process will exit with an error (unless
131133
// you catch it using a try/catch or "uncaughtException" handler)
134+
const NODE_ENV = env.get('NODE_ENV').asString()
132135
const PORT = env.get('PORT').required().asIntPositive()
133136

137+
// If mode is production then this is required, else use default
138+
const SECRET = env.get('SECRET', 'bad-secret').required(NODE_ENV === 'production').asString()
139+
134140
app.listen(PORT)
135141
```
136142

env-var.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ interface IOptionalVariable {
104104
convertFromBase64: () => IOptionalVariable
105105

106106
/**
107-
* Ensure the variable is set on process.env, if not an exception will be thrown.
107+
* Ensures the variable is set on process.env, if not an exception will be thrown.
108+
* Can pass false to bypass the check
108109
*/
109-
required: () => IPresentVariable;
110+
required: (isRequired?: boolean) => IPresentVariable;
110111

111112
/**
112113
* Attempt to parse the variable to a float. Throws an exception if parsing fails.

lib/variable.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,13 @@ module.exports = function getVariableAccessors (container, varName, defValue) {
9696
/**
9797
* Ensures a variable is set in the given environment container. Throws an
9898
* EnvVarError if the variable is not set or a default is not provided
99+
* @param {Boolean} isRequired
99100
*/
100-
required: function () {
101+
required: function (isRequired) {
102+
if (isRequired === false) {
103+
return accessors
104+
}
105+
101106
if (typeof container[varName] === 'undefined' && typeof defValue === 'undefined') {
102107
throw new EnvVarError(`"${varName}" is a required variable, but it was not set`)
103108
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "env-var",
3-
"version": "3.4.2",
3+
"version": "3.5.0",
44
"description": "Verification, sanatization, and type coercion for environment variables in Node.js",
55
"main": "env-var.js",
66
"typings": "env-var.d.ts",

test/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,12 @@ describe('env-var', function () {
355355
expect(mod.get('JSON').required().asJson()).to.be.an('object')
356356
})
357357

358+
it('should not throw if required is passed a false argument', function () {
359+
delete process.env.JSON
360+
361+
expect(mod.get('JSON').required(false).asJson()).to.equal(undefined)
362+
})
363+
358364
it('should throw an exception when required, but not set', function () {
359365
delete process.env.JSON
360366

0 commit comments

Comments
 (0)