valid-config

1.0.2 • Public • Published

valid-config

Define and validate configuration with an extended json schema.

Install

npm install --save valid-config

Usage

Inject env

const validConfig = require('valid-config');
process.env.FOO = 'bar';
 
const config = validConfig({
  type: 'object',
  properties: {
    foo: {
      type: 'string',
      env: 'FOO'
    }
  }
});
 
deepEqual(config, {
  foo: 'bar'
});

Load and merge

const validConfig = require('valid-config');
process.env.FOO = 'bar';
 
const configFile1 = {
  baz: 'xyz'
};
 
const configFile2 = {
  baz: 'jkw'
};
 
const config = validConfig({
  type: 'object',
  properties: {
    foo: {
      type: 'string',
      env: 'FOO'
    }
  }
}, configFile1, configFile2);
 
deepEqual(config, {
  foo: 'bar',
  baz: 'jkw'
});

Defaults

const validConfig = require('valid-config');
const config = validConfig({
  type: 'object',
  properties: {
    port: {
      type: 'number',
      env: 'PORT',
      default: 3000
    }
  }
});
 
deepEqual(config, {
  port: 3000
});

Env always takes precedence over the rest

process.env.PORT = '3001';
 
const configFile1 = {
  port: 8000
};
 
const validConfig = require('valid-config');
const config = validConfig({
  type: 'object',
  properties: {
    port: {
      type: 'number',
      env: 'PORT',
      default: 3000
    }
  }
});
 
deepEqual(config, {
  port: 3001
});

Normalize types

const validConfig = require('valid-config');
process.env.PORT = '3000';
 
const config = validConfig({
  type: 'object',
  properties: {
    port: {
      type: 'number',
      env: 'PORT'
    }
  }
});
 
deepEqual(config, {
  port: 3000
});

Load arrays from env

process.env.FOO = 'bar,baz';
 
const config = validConfig({
  type: 'object',
  properties: {
    foo: {
      type: 'array',
      items: {
        type: 'string'
      },
      env: 'FOO'
    }
  }
});
 
deepEqual(config, {
  foo: ['bar', 'baz']
});

Load arrays from env (specify the separator)

process.env.FOO = 'bar::baz';
 
const config = validConfig({
  type: 'object',
  properties: {
    foo: {
      type: 'array',
      items: {
        type: 'string'
      },
      env: 'FOO'
      envSeparator: '::'
    }
  }
});
 
deepEqual(config, {
  foo: ['bar', 'baz']
});

Validating required properties

throws(function() {
  validConfig({
    type: 'object',
    properties: {
      foo: {
        type: 'string',
        env: 'FOO'
      }
    },
    required: [
      'foo'
    ]
  });
});

Validating arrays

throws(function() {
  const configFile = {};
 
  validConfig({
    type: 'object',
    properties: {
      foo: {
        type: 'array',
        items: {
          type: 'string'
        },
        minItems: 1
      }
    }
  }, configFile);
});

Readme

Keywords

none

Package Sidebar

Install

npm i valid-config

Weekly Downloads

1

Version

1.0.2

License

MIT

Unpacked Size

10.5 kB

Total Files

15

Last publish

Collaborators

  • mcasimir