@moveaxlab/env-validation
v0.1.3
Published
Validate env variables
Downloads
342
Keywords
Readme
Env validation library
This started as a shameless ripoff of env-var, then took its own way.
This library exploits the moveax validation library to validate environment variables.
Installation
yarn add @moveaxlab/env-validation
Usage
Values can be retrieved from the environment specifying the type and, optionally, some rules, according to chainside validation library.
import { envValidation } from '@moveax/env-validation';
const my_var = envValidation.get('VARIABLE_NAME').asString();
const my_var_with_default = envValidation.get('VARIABLE_NAME', 'DEFAULT_VALUE').asString();
All variables are required by default.
If you want to make a variable optional, add the nullable
rule.
const my_var_possibly_null = envValidation.get('VARIABLE_NAME').asString('nullable');
If no value is given, no default value is specified, and the nullable
rule is present, the value returned will be undefined
.
String types, if given the nullable
rule, will return an empty string (''
) if no value is given for the variable.
Rules can be passed as an array of string or as a |
(pipe) separated string of rules.
The library throws an error in your face if the environment variable does not match the type.