Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/stefanwalther/env-val

Configuration management using environment-variables, validation & object extension.
https://github.com/stefanwalther/env-val

suspended

Last synced: about 5 hours ago
JSON representation

Configuration management using environment-variables, validation & object extension.

Awesome Lists containing this project

README

        

# env-val

> Configuration management using environment-variables, validation & object extension.

[![CircleCI](https://img.shields.io/circleci/project/github/stefanwalther/env-val.svg)](https://circleci.com/gh/stefanwalther/env-val)
[![codecov](https://codecov.io/gh/stefanwalther/env-val/branch/master/graph/badge.svg?token=dbOESk00SQ)](https://codecov.io/gh/stefanwalther/env-val)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fstefanwalther%2Fenv-val.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fstefanwalther%2Fenv-val?ref=badge_shield)

---

Using environment variables in node.js projects including **validation**, **default values** & **type conversion**.

## Install
```
$ npm install env-val --save
```

## Basic Usage
### Basic schema

Schemas are loaded by default from all files matching `**/*.js` in the directory `./config`.
Define a schema in `./config/logger.js`.

```js
// Load EnvVal to be able to get the exposed joi object.
const EnvVal = require('env-val');

const schema = EnvVal.joi.object({

LOGGER_LEVEL: EnvVal.joi
.string()
.valid(['error', 'warn', 'info', 'verbose', 'debug', 'silly'])
.default('info'),

LOGGER_ENABLED: EnvVal.joi
.boolean()
.truthy('TRUE')
.truthy('true')
.falsy('FALSE')
.falsy('false')
.default(true)

}).required();

const {error, value: envVars} = EnvVal
.joi
.validate(process.env, schema, {allowUnknown: true, stripUnknown: true});
if (error) {
throw new Error(`Config validation error: ${error.message}`);
}

const values = envVars;

module.exports = {
schema,
values
};

```

Basic initialization of `env-val`:

```js
const EnvVal = require('env-val');

let configs = new EnvVal().init();

console.log(configs.LOGGER_ENABLED); // => true
console.log(configs.LOGGER_LEVEL); // => 'info'

```

Override environment variables:

```js
const EnvVal = require('env-val');

let configs = new EnvVal({
LOGGER_ENABLED: false,
LOGGER_LEVEL: 'warn'
})
configs.init();

console.log(configs.LOGGER_ENABLED); // => false
console.log(configs.LOGGER_LEVEL); // => 'warn'
```

By default config files are loaded from the `./config` directory.
This can be customized by passing the `CONFIG_DIR` option to the constructor of _env-val_.

```js
const EnvVal = require('env-val');
const path = require('path');

let configs = new EnvVal({
CONFIG_DIR: path.join(__dirname, 'my-custom-dir`
});

```

## About
### Author
**Stefan Walther**

* [twitter](http://twitter.com/waltherstefan)
* [github.com/stefanwalther](http://github.com/stefanwalther)
* [LinkedIn](https://www.linkedin.com/in/stefanwalther/)

### Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/stefanwalther/env-val/issues). The process for contributing is outlined below:

1. Create a fork of the project
2. Work on whatever bug or feature you wish
3. Create a pull request (PR)

I cannot guarantee that I will merge all PRs but I will evaluate them all.

### License
MIT

***

_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 08, 2018._

[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fstefanwalther%2Fenv-val.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fstefanwalther%2Fenv-val?ref=badge_large)