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:
-
namestring | object -
String for Format name,
Object/Object[]or which contains arguments:-
name.namestring -
Format name
-
name.validatefunction -
See below
-
name.coercefunction -
See below
-
name.rewriteboolean -
See below
Sub properties
-
-
validateSchemaNode.validateCallback -
Validate function, should throw if the value is wrong
ErrororLISTOFERRORS(see example) -
coerceSchemaNode.coerce -
Coerce function to convert a value to a specified function (can be omitted)
-
rewriteboolean -
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:
-
formatsobject | Array.<object> -
Object containing list of Object
-
formats.{name}object -
{name} in
formats.{name}is the format name-
formats.{name}.validatefunction -
See Blueconfig.addFormat
-
formats.{name}.coercefunction -
See Blueconfig.addFormat
-
formats.{name}.rewriteboolean -
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:
-
namestring | object -
String for Getter name,
Object/Object[]or which contains arguments:-
name.namestring -
Getter name
-
name.getterfunction -
See below
-
name.usedOnlyOnceboolean -
See below
-
name.rewriteboolean -
See below
Sub properties
-
-
getterConfigObjectModel.getterCallback -
Getter function get external value depending of the name name.
-
usedOnlyOnceboolean -
falseby default. If true, The value can't be reused by anotherkeyname=value -
rewriteboolean -
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:
-
gettersobject | Array.<object> -
Object containing list of Getters/Object
-
getters.{name}object -
nameis the getter name-
getters.{name}.getterfunction -
See Blueconfig.addGetter
-
getters.{name}.usedOnlyOnceboolean -
See Blueconfig.addGetter
-
getters.{name}.rewriteboolean -
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:
-
parsersArray.<object> -
Parser
-
parsers.extensionstring -
Parser extension
-
parsers.parsefunction -
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:
-
rawSchemastring | object -
Schema object or a path to a schema JSON file
-
optionsobject
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:
-
newOrderArray.<string> -
Value of the property to validate
Returns:
- Type
- this