Njet-configuration

Configuration handler for njet

Download as .zip Download as .tar.gz View on GitHub

njet-configuration

Configuration handler for projects. Njet configuration is responsible for loading yaml configuration files.

Installation

npm install njet-configuration

Usage

First load njet configuration

var njetConfiguration = require('njet-configuration');

and create first configuration:

var config = njetConfiguration.create();

Getting data

To get Your configuration use:

config.get('x'); // to get specific value
config.getConfiguration(); // to get all values as json object

Set values from json objects

There are three ways to set configuration values.

config.set('x', 5);

will set "x" to 5. Now when You get Your configuration You will receive:

{ "x": 5 }

You can also replace whole configuration using load:

config.load({
    y: 6
});

This will replace Your configuration and You will receive:

{ "y": 6 }

Or You can merge two objects together:

config.merge({
    z: 1
});

To get:

{ "y": 6, "z": 1 }

Merging

Merge will always do deep copy of the objects. For example:

config.load({
    x: 1,
    y: ["a"]
});

config.merge({
    y: ["b"],
    z: 3
});

Will result:

{ "x": 1, "y": ["a", "b"], "z": 3 }

Validating

There are two ways to validate objects. You can add validators:

config.addValidator(function (config) {
    if (config.key === undefined) {
        return false;
    }
    return true;
});

Or schema:

config.schema({
    x: config.expect.number()
});

To validate configuration, simply use:

config.validate();
config.isValid();

For more information about schema validation visit https://github.com/cruks/cruks-lib-config

Yaml configuration files

You can load yaml files and directories containing yaml files like this:

config.load('/path/to/directory');
config.load('/path/to/file.yml');
config.merge('/path/to/another_file.yml');
config.merge('/path/to/another/directory');

Configuration

You cann pass options to njet configuration constructor. Like this:

var config = njetConfiguration.create({
    varbosity: 1,
    expect: expect,
    loader: loader
});