dotenv-safe icon indicating copy to clipboard operation
dotenv-safe copied to clipboard

Add `optionalValues` and `requiredValues` options

Open rigwild opened this issue 4 years ago • 0 comments

Closes #41, resolves #45

First, thank you for this lib, I use it on almost every projects of mine! 😊

This PR adds the options optionalValues and requiredValues. I thought it would be better to not allow allowEmptyValues to be a Set just like in #41, and make it new separate options.

How it works

  • Uses Set as mentioned in #41
  • optionalValues: Values in optionalValues will not be required anymore
  • allowEmptyValues: true + requiredValues: All values are optional except ones in requiredValues

Errors

  • allowEmptyValues: true + optionalValues: Error, useless as all values are already optional
  • allowEmptyValues: false + requiredValues: Error, everything is already required
  • optionalValues or requiredValues are not Set: Error, must be Set

How to use

// Any value can be empty except `THIS_ENV_IS_REQUIRED_ANYWAY` and `THIS_ONE_TOO`
require('dotenv-safe').config({
    allowEmptyValues: true,
    requiredValues: new Set(['THIS_ENV_IS_REQUIRED_ANYWAY', 'THIS_ONE_TOO']),
    example: './.my-env-example-filename'
});
// Every values are required except `THIS_ENV_IS_OPTIONAL` and `THIS_ONE_TOO`
require('dotenv-safe').config({
    optionalValues: new Set(['THIS_ENV_IS_OPTIONAL', 'THIS_ONE_TOO']),
    example: './.my-env-example-filename'
});

Let me know what you think of it 😄

rigwild avatar Mar 19 '21 14:03 rigwild