Define and validate configuration with an extended json schema.
npm install --save valid-config
const validConfig = require('valid-config');
process.env.FOO = 'bar';
const config = validConfig({
type: 'object',
properties: {
foo: {
type: 'string',
env: 'FOO'
}
}
});
deepEqual(config, {
foo: 'bar'
});
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'
});
const validConfig = require('valid-config');
const config = validConfig({
type: 'object',
properties: {
port: {
type: 'number',
env: 'PORT',
default: 3000
}
}
});
deepEqual(config, {
port: 3000
});
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
});
const validConfig = require('valid-config');
process.env.PORT = '3000';
const config = validConfig({
type: 'object',
properties: {
port: {
type: 'number',
env: 'PORT'
}
}
});
deepEqual(config, {
port: 3000
});
process.env.FOO = 'bar,baz';
const config = validConfig({
type: 'object',
properties: {
foo: {
type: 'array',
items: {
type: 'string'
},
env: 'FOO'
}
}
});
deepEqual(config, {
foo: ['bar', 'baz']
});
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']
});
throws(function() {
validConfig({
type: 'object',
properties: {
foo: {
type: 'string',
env: 'FOO'
}
},
required: [
'foo'
]
});
});
throws(function() {
const configFile = {};
validConfig({
type: 'object',
properties: {
foo: {
type: 'array',
items: {
type: 'string'
},
minItems: 1
}
}
}, configFile);
});