new BlueconfigCore()
Cutomize Blueconfig before make a ConfigObjectModel (= COM, Like DOM but not for Document, for Config)
Example
const Blueconfig = require('blueconfig/lib/core.js')
const blueconfig = new Blueconfig()
console.log(blueconfig.getGettersOrder()) // === [value', 'force']
Methods
addFormat(name, validate, coerce, rewriteopt) → {this}
Adds a new custom format. Validate function and coerce function will be used to validate COM property with format
type.
Example
Blueconfig.addFormat({
name: 'int',
coerce: (value) => (typeof value !== 'undefined') ? parseInt(value, 10) : value,
validate: function(value) {
if (Number.isInteger(value)) {
throw new Error('must be an integer')
}
}
})
Parameters:
-
name
string | object -
String for Format name,
Object/Object[]
or which contains arguments:-
name.name
string -
Format name
-
name.validate
function -
See below
-
name.coerce
function -
See below
-
name.rewrite
boolean -
See below
Sub properties
-
-
validate
SchemaNode.validateCallback -
Validate function, should throw if the value is wrong
Error
orLISTOFERRORS
(see example) -
coerce
SchemaNode.coerce -
Coerce function to convert a value to a specified function (can be omitted)
-
rewrite
boolean -
Allow rewrite an existant format
Returns:
- Type
- this
addFormats(formats) → {this}
Adds several formats
Example
// Add several formats with: [Object, Object, Object, Object]
const vFormat = require('blueconfig-format-with-validator')
blueconfig.addFormats({ // add: email, ipaddress, url, token
email: vFormat.email,
ipaddress: vFormat.ipaddress,
url: vFormat.url,
token: {
validate: function(value) {
if (!isToken(value)) {
throw new Error(':(')
}
}
}
})
Parameters:
-
formats
object | Array.<object> -
Object containing list of Object
-
formats.{name}
object -
{name} in
formats.{name}
is the format name-
formats.{name}.validate
function -
See Blueconfig.addFormat
-
formats.{name}.coerce
function -
See Blueconfig.addFormat
-
formats.{name}.rewrite
boolean -
See Blueconfig.addFormat
Sub properties
-
Sub properties
-
Returns:
- Type
- this
addGetter(name, getter, usedOnlyOnceopt, rewriteopt) → {this}
Adds a new custom getter. Getter function get value depending of the property name. In schema, the property name is a keyname of the schema.
Example
convict.addGetter('aws', function(aws, schema, stopPropagation) {
return aws.get(aws)
}, false, true)
Parameters:
-
name
string | object -
String for Getter name,
Object/Object[]
or which contains arguments:-
name.name
string -
Getter name
-
name.getter
function -
See below
-
name.usedOnlyOnce
boolean -
See below
-
name.rewrite
boolean -
See below
Sub properties
-
-
getter
ConfigObjectModel.getterCallback -
Getter function get external value depending of the name name.
-
usedOnlyOnce
boolean -
false
by default. If true, The value can't be reused by anotherkeyname=value
-
rewrite
boolean -
Allow rewrite an existant format
Returns:
- Type
- this
addGetters(getters) → {this}
Adds several getters
Example
// Example with the default env & arg getters:
Blueconfig.addGetters({
env: {
getter: (value, schema) => schema._cvtCoerce(this.getEnv()[value])
},
arg: {
getter: function(value, schema, stopPropagation) {
const argv = parseArgs(this.getArgs(), { configuration: { 'dot-notation': false } })
return schema._cvtCoerce(argv[value])
},
usedOnlyOnce: true
}
})
Parameters:
-
getters
object | Array.<object> -
Object containing list of Getters/Object
-
getters.{name}
object -
name
is the getter name-
getters.{name}.getter
function -
See Blueconfig.addGetter
-
getters.{name}.usedOnlyOnce
boolean -
See Blueconfig.addGetter
-
getters.{name}.rewrite
boolean -
See Blueconfig.addGetter
Sub properties
-
Sub properties
-
Returns:
- Type
- this
addParser(parsers) → {this}
Adds new custom file parsers. JSON.parse will be used by default for unknown extension (default extension -> *
=> JSON).
Blueconfig is able to parse files with custom file types during merge
. For this specify the
corresponding parsers with the associated file extensions.
If no supported extension is detected, merge
will fallback to using the default json parser JSON.parse
.
Example
blueconfig.addParser([
{ extension: ['yml', 'yaml'], parse: yaml.safeLoad },
{ extension: ['yaml', 'yml'], parse: require('yaml').safeLoad },
// will allow comment in json file
{ extension: 'json', parse: require('json5').parse } // replace `JSON` by `json5`
])
config.merge('config.yml')
Parameters:
-
parsers
Array.<object> -
Parser
-
parsers.extension
string -
Parser extension
-
parsers.parse
function -
Parser function
Sub properties
-
Returns:
- Type
- this
getGettersOrder() → {Array.<string>}
Gets array with getter name in the current order of priority
Example
blueconfig.getGettersOrder() // ['default', 'value', 'env', 'arg', 'force']
Returns:
Returns current getter order
- Type
- Array.<string>
init(rawSchema, optionsopt) → {Array.<string>}
Gets array with getter name in the current order of priority
Example
const Blueconfig = require('blueconfig/lib/core.js')
const blueconfig = new Blueconfig()
blueconfig.init(rawSchema, options)
Parameters:
-
rawSchema
string | object -
Schema object or a path to a schema JSON file
-
options
object
Returns:
Returns current getter order
- Type
- Array.<string>
initPerformer() → {this}
Create instance of worker (Parser, Getter, Ruler)
Returns:
- Type
- this
sortGetters(newOrder) → {this}
Sorts getter priority, this function uses ascending order. It is recommanded to uses this function before init COM (with blueconfig()
)
or you should call <COM>.refreshGetters()
.
Example
blueconfig.getGettersOrder()
// ['default', 'value', 'env', 'arg', 'force']
// two ways to do:
blueconfig.sortGetters(['default', 'value', 'arg', 'env', 'force'])
blueconfig.sortGetters(['default', 'value', 'arg', 'env']) // force is optional and must be the last one
blueconfig.getGettersOrder()
// ['default', 'value', 'arg', 'env', 'force']
Parameters:
-
newOrder
Array.<string> -
Value of the property to validate
Returns:
- Type
- this