app-etc-config
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -7,3 +7,4 @@ 'use strict'; | ||
merge = require( 'utils-merge2' )(), | ||
validate = require( './validate.js' ), | ||
validator = require( 'is-my-json-valid' ), | ||
validate = require( './validate.opts.js' ), | ||
db = require( './db.js' ); | ||
@@ -27,2 +28,5 @@ | ||
* @param {Boolean} [options.create=true] - boolean indicating whether to create a keypath if it does not exist | ||
* @param {Object} [options.schema] - configuration JSON schema | ||
* @param {Object} [options.formats] - custom validation formats | ||
* @param {Object} [options.ext] - external schemas | ||
* @returns {Config} Config instance | ||
@@ -47,2 +51,13 @@ */ | ||
} | ||
if ( opts.schema !== void 0 ) { | ||
this._validate = validator( opts.schema, { | ||
'formats': opts.formats || {}, | ||
'schemas': opts.ext || {}, | ||
'greedy': true, | ||
'verbose': true | ||
}); | ||
delete opts.schema; | ||
delete opts.formats; | ||
delete opts.ext; | ||
} | ||
this._opts = merge( {}, DEFAULTS, opts ); | ||
@@ -64,2 +79,3 @@ this._name = typeName( this ); | ||
Config.prototype.load = require( './load.js' ); | ||
Config.prototype.validate = require( './validate.js' ); | ||
@@ -66,0 +82,0 @@ |
@@ -5,5 +5,3 @@ 'use strict'; | ||
var isObject = require( 'validate.io-object' ), | ||
isString = require( 'validate.io-string-primitive' ), | ||
isBoolean = require( 'validate.io-boolean-primitive' ); | ||
var isFunction = require( 'validate.io-function' ); | ||
@@ -14,28 +12,25 @@ | ||
/** | ||
* FUNCTION: validate( opts, options ) | ||
* Validates function options. | ||
* FUNCTION: validate( [validator] ) | ||
* Validates a configuration store. | ||
* | ||
* @param {Object} opts - destination object | ||
* @param {Object} options - options to validate | ||
* @param {String} [options.sep] - keypath separator | ||
* @param {Boolean} [options.create] - boolean indicating whether to create a keypath if it does not exist | ||
* @returns {Error|Null} error or null | ||
* @param {Function} [validator] - JSON schema validator | ||
* @returns {Boolean|Object[]|*} validation results | ||
*/ | ||
function validate( opts, options ) { | ||
if ( !isObject( options ) ) { | ||
return new TypeError( 'invalid input argument. Options argument must be an object. Value: `' + options + '`.' ); | ||
} | ||
if ( options.hasOwnProperty( 'sep' ) ) { | ||
opts.sep = options.sep; | ||
if ( !isString( opts.sep ) ) { | ||
return new TypeError( 'invalid option. Separator option must be a primitive string. Option: `' + opts.sep + '`.' ); | ||
function validate( validator ) { | ||
/* jshint validthis:true */ | ||
var out; | ||
if ( arguments.length ) { | ||
if ( !isFunction( validator ) ) { | ||
throw new TypeError( 'invalid input argument. Input argument must be a function. Value: `' + validator + '`.' ); | ||
} | ||
return validator( this._db ); | ||
} | ||
if ( options.hasOwnProperty( 'create' ) ) { | ||
opts.create = options.create; | ||
if ( !isBoolean( opts.create ) ) { | ||
return new TypeError( 'invalid option. Create option must be a primitive boolean. Option: `' + opts.create + '`.' ); | ||
} | ||
if ( this._validate === void 0 ) { | ||
return true; | ||
} | ||
return null; | ||
out = this._validate( this._db ); | ||
if ( out === false ) { | ||
return this._validate.errors; | ||
} | ||
return out; | ||
} // end FUNCTION validate() | ||
@@ -42,0 +37,0 @@ |
{ | ||
"name": "app-etc-config", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Creates a configuration API.", | ||
@@ -48,3 +48,4 @@ "author": { | ||
"dependencies": { | ||
"app-etc-load": "0.0.0", | ||
"app-etc-load": "^1.0.1", | ||
"is-my-json-valid": "^2.12.2", | ||
"type-name": "^1.0.1", | ||
@@ -56,2 +57,3 @@ "utils-copy": "^1.0.0", | ||
"validate.io-boolean-primitive": "^1.0.0", | ||
"validate.io-function": "^1.0.2", | ||
"validate.io-object": "^1.0.4", | ||
@@ -58,0 +60,0 @@ "validate.io-string-primitive": "^1.0.0" |
@@ -33,2 +33,4 @@ Config | ||
* __create__: `boolean` indicating whether to create a keypath if it does not exist. See [utils-deep-set](https://github.com/kgryte/utils-deep-set). Default: `true`. | ||
* __schema__: JSON [schema](http://json-schema.org/) for validating a configuration. | ||
* __formats__: JSON [schema](http://json-schema.org/) custom formats (see [is-my-json-valid](https://github.com/mafintosh/is-my-json-valid#custom-formats)). | ||
@@ -40,6 +42,11 @@ To specify `options`, | ||
'sep': '|', | ||
'create': false | ||
'create': false, | ||
'schema': require( '/path/to/schema.json' ), | ||
'formats': { | ||
'only-a': /^a+$/ | ||
} | ||
}); | ||
``` | ||
=== | ||
##### config.set( keypath, value[, options] ) | ||
@@ -70,2 +77,3 @@ | ||
=== | ||
##### config.merge( [keypath,] config[, options] ) | ||
@@ -136,3 +144,3 @@ | ||
=== | ||
##### config.get( [ keypath[, options] ] ) | ||
@@ -176,2 +184,3 @@ | ||
=== | ||
##### config.clone( [keypath[, options] ] ) | ||
@@ -235,2 +244,3 @@ | ||
=== | ||
##### config.load( filename ) | ||
@@ -253,4 +263,50 @@ | ||
=== | ||
##### config.validate( [validator] ) | ||
Validates a configuration. | ||
``` javascript | ||
// Valid configuration: | ||
var out = config.validate(); | ||
// returns true | ||
``` | ||
If a configuration is invalid, the method returns an `array` containing validation errors. | ||
``` javascript | ||
// Invalid configuration: | ||
out = config.validate(); | ||
// returns [{...},{...},...] | ||
``` | ||
The method accepts a `validator` function, which can be useful for validating against multiple schemas or when a `schema` was not provided during initialization. The `validator` should accept as its first argument the configuration `object` to be validated. The method returns validation results without modification. | ||
``` javascript | ||
// Example JSON schema validator: | ||
var validator = require( 'is-my-json-valid' ); | ||
var schema = require( '/path/to/schema.json' ); | ||
var validate = validator( schema, { | ||
'verbose': true, | ||
'greedy': true | ||
}); | ||
var out = config.validate( validate ); | ||
console.log( out ); | ||
console.log( validate.errors ); | ||
``` | ||
If a `schema` option was __not__ provided during initialization and a `validator` is __not__ provided at runtime, the method __always__ returns `true`. | ||
``` javascript | ||
var config = etc(); | ||
config.set( 'port', 80 ); | ||
var out = config.validate(); | ||
// returns true | ||
``` | ||
=== | ||
#### etc.exts() | ||
@@ -257,0 +313,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25294
14
414
455
11
+ Addedis-my-json-valid@^2.12.2
+ Addedvalidate.io-function@^1.0.2
+ Addedapp-etc-load@1.0.1(transitive)
+ Addedcjson@0.3.3(transitive)
+ Addedgenerate-function@2.3.1(transitive)
+ Addedgenerate-object-property@1.2.0(transitive)
+ Addedhjson@1.8.4(transitive)
+ Addedis-my-ip-valid@1.0.1(transitive)
+ Addedis-my-json-valid@2.20.6(transitive)
+ Addedis-property@1.0.2(transitive)
+ Addedjju@1.4.0(transitive)
+ Addedjson-parse-helpfulerror@1.0.3(transitive)
+ Addedjson5@0.4.0(transitive)
+ Addedjsonpointer@5.0.1(transitive)
+ Addedproperties@1.2.1(transitive)
+ Addedutils-cjson-parse@1.0.0(transitive)
+ Addedutils-hjson-parse@1.0.1(transitive)
+ Addedutils-ini-parse@1.0.0(transitive)
+ Addedutils-json5-parse@1.0.0(transitive)
+ Addedutils-properties-parse@1.0.0(transitive)
+ Addedxtend@4.0.2(transitive)
- Removedapp-etc-load@0.0.0(transitive)
Updatedapp-etc-load@^1.0.1