1.3.0 • Published 4 years ago

eznv v1.3.0

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

NPM Build Status Coverage Status

EZNVEasy Env Management

Getting started

Install the package

npm i --save eznv

Define a Schema

EZNV uses a schema object to validate an env file and to determine the type of the resulting object.

  import EZ from 'eznv'

  const schema = EZ.Schema({
    NUM: EZ.Number({
      default: 0,
      minimum: 0,
      maximum: 10
    })
  })

Load the ENV

  const config = await schema.load()

  // or to load synchronously

  const config = schema.loadSync()

Load Options

The following are options passable to schema.load and schema.loadSync.

optiontypedefaultdescription
cwdstringprocess.cwd()Path to the default directory to search for your schema file and env file
filestring.envPath, relative to cwd or absolute, to the env file
modefile_first | no_file | file_onlyfile_firstconfigure whether use the file only, process.env only, or both
matchCasebooleanfalseuse exact case matching for variable names. Ignores case by default.

Schema Properties

The EZ.Schema function takes as an argument a dictionary of property definitions. There are four different property definitions you can create. EZ.Number, EZ.String, EZ.Integer, and EZ.Boolean. Each type allows for different options to control validation.

Common Validator Properties

OptionDescription
defaultA default value to use when the property is missing
requiredShould the property be required. Defaults to true

Number Validator Properties

EZ.Number will define a number property.

OptionDescription
minimumThe value must be greater than or equal to this number.
exclusiveMinimumThe value must be strictly greater than this number.
maximumThe value must be less than or equal to this number.
exclusiveMaximumThe value must be strictly less than this number.

Integer Validator Properties

EZ.Integer will define an integer property.

OptionDescription
minimumThe value must be greater than or equal to this number.
maximumThe value must be less than or equal to this number.

String Validator Properties

EZ.String will define a string property.

OptionDescription
minLengthThe value must have length greater than or equal to this number.
maxLengthThe value must have length less than or equal to this number.
patternThe value must match this regular expression

Boolean Validator Properties

EZ.Boolean will define a string property.

OptionDescription
truthyValuesAn array of strings to consider true
falseyValuesAn array of strings to consider false
strictCaseShould matching against truthy and falsey values consider case? Defaults to false

Env Files

The supported format for env files is fairly standard. Here's a quick overview.

Variables

Variable names must start with a letter and contain only letters, numbers, and underscores.

Assignments

Setting a value to a variable in the env file is done in bash style, with the variable name on the left, an equal sign, and the value on the right.

SOME_STRING=this is a value

Values

The right side of an assignment can take two different forms; a literal string or a quoted string.

Quoted Values

Quoted values begin and end with either a single or double quote. By defining a value within quotes it will be allowed to include the escaped character sequences for newline ( \n ), tab ( \t ), character-return ( \r ), form-feed ( \f ), and unicode character ( \u0000 ) (where 0000 is a hex sequence for the desired character). This also means that the escape character ( \ ) and either double ( " ) or single quote ( ' ) character must be escaped in quoted values. Finally, the dollar sign ( $ ) can be escaped to prevent it being used for variable substitution.

Literal Values

These are interpreted without any escape sequences or variable substitution. They are also trimmed of any whitespace preceeding or following the value.

Variable Substitution

Quoted values can include other variables either from within the env file or from the system environment. To do this, the dollar sign character ( $ ) is used to indicate a substitution when followed by a valid variable name.

OTHER_STRING="SOME VALUE"
SOME_STRING="$OTHER_STRING"

# Gives SOME_STRING === "SOME VALUE"

When the position of the substitution is immeditately followed by other valid variable name characters, you will need to wrap the variable name in curly braces to distinguish it.

OTHER_STRING="SOME "
SOME_STRING="${OTHER_STRING}VALUE"

# Gives SOME_STRING === "SOME VALUE"

Comments

Anything to the right of a hash sign ( # ) are interpreted as comments and ignored by the parser, as long as the hash is not within quotes.

SOME_STRING="# this is not ignored" # this is ignored

# this is also ignored
1.3.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago

0.5.0

4 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago