Skip to content

Commit

Permalink
Missing values should default to empty strings for errorOnRegex
Browse files Browse the repository at this point in the history
  • Loading branch information
FokkeZB committed Sep 25, 2020
1 parent 43e550d commit 61b99ed
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const config = options => {
if (options.errorOnRegex) {
const regexMismatchKeys = schemaKeys.filter(function (key) {
if (schema[key]) {
return !new RegExp(schema[key]).test(config[key]);
return !new RegExp(schema[key]).test(typeof config[key] === 'string' ? config[key] : '');
}
});

Expand Down
5 changes: 5 additions & 0 deletions test/.env.schema.regex-optional
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
TEST_VAR=^my test var$
TEST_ONE=^overridden$

TEST_MISSING_OPTIONAL=^(optional)?$
TEST_MISSING_REQUIRED=^optional$
10 changes: 10 additions & 0 deletions test/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ describe('dotenv-extended tests', () => {
expect(runTest).to.throw('REGEX MISMATCH: TEST_TWO, TEST_THREE');
});

it('Should default missing values to empty string when errorOnRegex is true', () => {
const runTest = () => {
dotenvex.load({
schema: '.env.schema.regex-optional',
errorOnRegex: true,
});
};
expect(runTest).to.throw('REGEX MISMATCH: TEST_MISSING_REQUIRED');
});

it('Should log an error when silent is set to false and .env.defaults is missing', function () {
dotenvex.load({silent: false});
expect(console.error).to.have.been.calledOnce;
Expand Down

0 comments on commit 61b99ed

Please sign in to comment.