Skip to content
Hristijan Lubeski edited this page Mar 21, 2019 · 1 revision

Welcome to the Envaridator docs.

Brief introduction

Envaridator is a tiny library used as MITM between the environment variables and the validation of their value.

To get you up and running, you'll need an instance of envaridator. To create an instance, envaridator ask for the name of the environment variable, a validator - so the value of the environment variable will be validated against, and, a description for the environment variable.

Envar

When an environment variable is being registered, it's name, description and validator are being stored in an object for later use. Envar has few getters:

  • name - returns the name of the environment variable
const envVar = new Envar("ENV_VAR", toi.required().and(toi.str.is()), "some description");

console.log(envVar.name) // => ENV_VAR
  • description - returns the description of the environment variable.
const envVar = new Envar("ENV_VAR", toi.required().and(toi.str.is()), "some description");

console.log(envVar.description) // => some description
  • value - calling value actually calls the validator against the value of the environment variable. If everything goes correctly, it returns value. After the first call of value (if the validation passes without any errors), the value returned will be cached so you don't need to validate the value every time you for it.
process.env['KEY'] = 'value';
const envVar = new Envar("KEY", toi.required().and(toi.str.is()), "some description");

console.log(envVar.value) // => value

Envaridator

  • register is a method which basically "registers" an environment variable in envaridator, alongside a validator and description and are kept in an object which key's are the environment variable names, and values are Envar instances.
const envVar = envaridator.register(<NAME>, <VALIDATOR>, <DESCRIPTION>); // `envVar` would be an instance of Envar.
  • validate is a method which iterates over the registered environment variables and validates them one by one. If an validation fails, it keeps the name of the failed environment variable and the reason of the failure until it finishes iterating. At the end, it returns a human-readable status report for the failed variables.
const envVar = envaridator.register("ENV_VAR", toi.required().and(toi.str.is()), "pretty neat description");

try {
    envaridator.validate();
} catch (err) {
    console.log(err.message);
}
  • describeAll - returns the names of the registered environment variables and their description.
const envVar = envaridator.register("ENV_VAR", toi.required().and(toi.str.is()), "pretty neat description");

console.log(envaridator.describeAll()); 

Examples

Please check the examples folder for some examples of how envaridator is being used.

Clone this wiki locally