options-config

Validate and replace your default configuration options with ease.

Usage no npm install needed!

<script type="module">
  import optionsConfig from 'https://cdn.skypack.dev/options-config';
</script>

README

options-config

Validate and replace your default configuration options with ease.

The validation scripts for configuration objects are harder to write than it seems. Luckily for you, options-config was released. This tiny library allows you to add a configuration object, of up to 2 levels, into your JavaScript library. This way, without you worrying about the validation mechanisms, your user will be able to replace any configuration option with any valid value.

Build status Dependencies status Version License

Installation

Open Terminal and install the package with this command:

npm install options-config --save

Then import options-config into nay file you are planning to use it:

import OptionsConfig from 'options-config';

Finally, do the one time setup:

const options = new OptionsConfig([defaultsObject]);

Defaults’ object

The defaults’ object is an object containing the default values and restrictions for every configuration option.

Key Type Description
default string, object The result when there isn’t any valid value given.
type string, array Only the values of this type will be valid.
valid string, array, object The only values that will be valid.
regex regexp A RegExp expression that has to match with the given value.
range object The min, max and step parameters for numbers.

Practical examples:

const defaultsObject = {
  x: {
    default: 10,
    type: ['number', 'boolean']
  },
  y: {
    default: 'foo',
    type: 'string',
    valid: ['foo', 'bar', 'hello', 'world']
  }
};
const defaultsObject = {
  x: true,
  y: {
    default: {
      y1: 'foo',
      y2: 'bar'
    },
    type: 'string',
    regex: /[A-z]{3}/
  }
};
const defaultsObject = {
  x: {
    x1: {
      default: 15,
      type: 'number',
      range: {
        min: 0,
        max: 100,
        step: 5
      }
    },
    x2: {
      default: false,
      type: 'boolean'
    }
  },
  y: {
    default: 200,
    valid: {
      number: 'all',
      string: ['e', 'π', 'pi']
    }
  }
};

Validate

options.validate(object, [defaultsObject])

Returns an object with all the configuration options, whether they are the default value or the one given by the user.

Parameter Required Description
object true The configuration object given by the user. It can contain none, some or all of the available options.
defaultsObject false The default values and restrictions. It’s not required if it has already been declared in the setup.

Practical examples:

const userOptions = options.validate({
  x: false,
  y: 'hello'
});
const userOptions = options.validate({
  x: 15
}, {
  x: {
    default: 60,
    type: 'number'
  }
});

About

Contributing

If you have any trouble while installing or using options-config, or you want to suggest a change, I encourage you to open an issue or make a pull request. A short explanation is enough, and it will improve this project for you and other developers.

Tests

To run the tests, first install the dev dependencies and then run the test command:

npm install -d && npm test

License

© 2018, Nil Vila. Released under the MIT License.