mastercheck
Advanced tools
Comparing version 0.2.7 to 0.2.8
@@ -46,5 +46,6 @@ /** | ||
Number: function(option) { | ||
this.option = option || {}; | ||
this.check = function(data) { | ||
if (!this.option.required && data === undefined) { | ||
this.option = option; | ||
this.check = function(data, parents) { | ||
var _option = (typeof this.option === 'function' ? this.option(parents) : this.option) || {}; | ||
if (!_option.required && data === undefined) { | ||
return; | ||
@@ -58,13 +59,13 @@ } | ||
} | ||
if (this.option.hasOwnProperty('min') && this.option.min > data) { | ||
return data + ' should be above ' + this.option.min; | ||
if (_option.hasOwnProperty('min') && _option.min > data) { | ||
return data + ' should be above ' + _option.min; | ||
} | ||
if (this.option.hasOwnProperty('max') && this.option.max < data) { | ||
return data + ' should be below ' + this.option.max; | ||
if (_option.hasOwnProperty('max') && _option.max < data) { | ||
return data + ' should be below ' + _option.max; | ||
} | ||
if (this.option.integer && data % 1 !== 0) { | ||
if (_option.integer && data % 1 !== 0) { | ||
return data + ' should be integer'; | ||
} | ||
if (this.option.hasOwnProperty('select') && !~this.option.select.indexOf(data)) { | ||
return data + ' should match ' + this.option.select.join(' or '); | ||
if (_option.hasOwnProperty('select') && !~_option.select.indexOf(data)) { | ||
return data + ' should match ' + _option.select.join(' or '); | ||
} | ||
@@ -87,5 +88,6 @@ }; | ||
String: function(option) { | ||
this.option = option || {}; | ||
this.check = function(data) { | ||
if (!this.option.required && data === undefined) { | ||
this.option = option; | ||
this.check = function(data, parents) { | ||
var _option = (typeof this.option === 'function' ? this.option(parents) : this.option) || {}; | ||
if (!_option.required && data === undefined) { | ||
return; | ||
@@ -99,13 +101,13 @@ } | ||
} | ||
if (this.option.hasOwnProperty('minLength') && this.option.minLength > data.length) { | ||
return data + ' length should be above ' + this.option.minLength; | ||
if (_option.hasOwnProperty('minLength') && _option.minLength > data.length) { | ||
return data + ' length should be above ' + _option.minLength; | ||
} | ||
if (this.option.hasOwnProperty('maxLength') && this.option.maxLength < data.length) { | ||
return data + ' length should be below ' + this.option.maxLength; | ||
if (_option.hasOwnProperty('maxLength') && _option.maxLength < data.length) { | ||
return data + ' length should be below ' + _option.maxLength; | ||
} | ||
if (this.option.hasOwnProperty('match') && !data.match(this.option.match)) { | ||
return data + ' should match ' + this.option.match; | ||
if (_option.hasOwnProperty('match') && !data.match(_option.match)) { | ||
return data + ' should match ' + _option.match; | ||
} | ||
if (this.option.hasOwnProperty('select') && !~this.option.select.indexOf(data)) { | ||
return data + ' should match ' + this.option.select.join(' or '); | ||
if (_option.hasOwnProperty('select') && !~_option.select.indexOf(data)) { | ||
return data + ' should match ' + _option.select.join(' or '); | ||
} | ||
@@ -124,5 +126,6 @@ }; | ||
Boolean: function(option) { | ||
this.option = option || {}; | ||
this.check = function(data) { | ||
if (!this.option.required && data === undefined) { | ||
this.option = option; | ||
this.check = function(data, parents) { | ||
var _option = (typeof this.option === 'function' ? this.option(parents) : this.option) || {}; | ||
if (!_option.required && data === undefined) { | ||
return; | ||
@@ -149,5 +152,6 @@ } | ||
Object: function(option, format) { | ||
this.option = option || {}; | ||
this.check = function(data) { | ||
if (!this.option.required && data === undefined) { | ||
this.option = option; | ||
this.check = function(data, parents) { | ||
var _option = (typeof this.option === 'function' ? this.option(parents) : this.option) || {}; | ||
if (!_option.required && data === undefined) { | ||
return; | ||
@@ -177,5 +181,6 @@ } | ||
Array: function(option, format) { | ||
this.option = option || {}; | ||
this.check = function(data) { | ||
if (!this.option.required && data === undefined) { | ||
this.option = option; | ||
this.check = function(data, parents) { | ||
var _option = (typeof this.option === 'function' ? this.option(parents) : this.option) || {}; | ||
if (!_option.required && data === undefined) { | ||
return; | ||
@@ -189,7 +194,7 @@ } | ||
} | ||
if (this.option.hasOwnProperty('minLength') && this.option.minLength > data.length) { | ||
return data + ' length should be above ' + this.option.minLength; | ||
if (_option.hasOwnProperty('minLength') && _option.minLength > data.length) { | ||
return data + ' length should be above ' + _option.minLength; | ||
} | ||
if (this.option.hasOwnProperty('maxLength') && this.option.maxLength < data.length) { | ||
return data + ' length should be below ' + this.option.maxLength; | ||
if (_option.hasOwnProperty('maxLength') && _option.maxLength < data.length) { | ||
return data + ' length should be below ' + _option.maxLength; | ||
} | ||
@@ -211,12 +216,13 @@ }; | ||
Map: function(option, format) { | ||
this.option = option || {}; | ||
this.check = function(data) { | ||
if (this.option.hasOwnProperty('maxLength') && this.option.maxLength < data.length) { | ||
return data + ' length should be above ' + this.option.maxLength; | ||
this.option = option; | ||
this.check = function(data, parents) { | ||
var _option = (typeof this.option === 'function' ? this.option(parents) : this.option) || {}; | ||
if (_option.hasOwnProperty('maxLength') && _option.maxLength < data.length) { | ||
return data + ' length should be above ' + _option.maxLength; | ||
} | ||
if (this.option.hasOwnProperty('match') && !data.match(this.option.match)) { | ||
return data + ' should match ' + this.option.match; | ||
if (_option.hasOwnProperty('match') && !data.match(_option.match)) { | ||
return data + ' should match ' + _option.match; | ||
} | ||
if (this.option.hasOwnProperty('select') && !~this.option.select.indexOf(data)) { | ||
return data + ' should match ' + this.option.select; | ||
if (_option.hasOwnProperty('select') && !~_option.select.indexOf(data)) { | ||
return data + ' should match ' + _option.select; | ||
} | ||
@@ -246,73 +252,49 @@ }; | ||
* @param {Object|Array} master | ||
* @param {Array} parents | ||
*/ | ||
MasterCheck.prototype._check = function(format, master) { | ||
MasterCheck.prototype._check = function(format, master, parents) { | ||
parents = parents || []; | ||
var errs = []; | ||
var i, j, key, _key, _errs, err; | ||
if (Array.isArray(format)) { | ||
for (i = 0; i < master.length; i++) { | ||
if (format[0] instanceof this._formatTypes.Number || | ||
format[0] instanceof this._formatTypes.String || | ||
format[0] instanceof this._formatTypes.Boolean) { | ||
err = format[0].check(master[i]); | ||
if (err) { | ||
errs.push({ key: i, value: master[i], message: err }); | ||
} | ||
} else if (format[0] instanceof this._formatTypes.Object) { | ||
err = format[0].check(master[i]); | ||
if (err) { | ||
errs.push({ key: i, value: master[i], message: err }); | ||
continue; | ||
} | ||
_errs = this._check(format[0].format, master[i]); | ||
for (j = 0; j < _errs.length; j++) { | ||
errs.push({ key: i + '.' + _errs[j].key, value: _errs[j].value, message: _errs[j].message }); | ||
} | ||
} else if (typeof format[0] === typeof master[i] && Array.isArray(format[0]) === Array.isArray(master[i])) { | ||
_errs = this._check(format[0], master[i]); | ||
for (j = 0; j < _errs.length; j++) { | ||
errs.push({ key: i + '.' + _errs[j].key, value: _errs[j].value, message: _errs[j].message }); | ||
} | ||
} else if (typeof master[i] !== 'undefined') { | ||
errs.push({ key: i, value: JSON.stringify(master[i]), message: 'Format Error.' }); | ||
var key, i, _key, err, _errs, _format, _master, _parents; | ||
var isArrayFormat = Array.isArray(format); | ||
for (key in isArrayFormat ? master : format) { | ||
_format = isArrayFormat ? format[0] : format[key]; | ||
_master = master[key]; | ||
_parents = [ { parent: master, key: key } ].concat(parents); | ||
if (_format instanceof this._formatTypes.Number || | ||
_format instanceof this._formatTypes.String || | ||
_format instanceof this._formatTypes.Boolean) { | ||
err = _format.check(_master, _parents); | ||
if (err) { | ||
errs.push({ key: key, value: _master, message: err }); | ||
} | ||
} | ||
} else { | ||
for (key in format) { | ||
if (format[key] instanceof this._formatTypes.Number || | ||
format[key] instanceof this._formatTypes.String || | ||
format[key] instanceof this._formatTypes.Boolean) { | ||
err = format[key].check(master[key]); | ||
} else if (_format instanceof this._formatTypes.Object) { | ||
err = _format.check(_master, _parents); | ||
if (err) { | ||
errs.push({ key: key, value: _master, message: err }); | ||
continue; | ||
} | ||
_errs = this._check(_format.format, _master, _parents); | ||
for (i = 0; i < _errs.length; i++) { | ||
errs.push({ key: key + '.' + _errs[i].key, value: _errs[i].value, message: _errs[i].message }); | ||
} | ||
} else if (_format instanceof this._formatTypes.Map) { | ||
for (_key in _master) { | ||
err = _format.check(_key, _parents); | ||
if (err) { | ||
errs.push({ key: key, value: master[key], message: err }); | ||
} | ||
} else if (format[key] instanceof this._formatTypes.Object) { | ||
err = format[key].check(master[key]); | ||
if (err) { | ||
errs.push({ key: key, value: master[key], message: err }); | ||
errs.push({ key: key, value: _key, message: err }); | ||
continue; | ||
} | ||
_errs = this._check(format[key].format, master[key]); | ||
_errs = this._check(_format.format, _master[_key], _parents); | ||
for (i = 0; i < _errs.length; i++) { | ||
errs.push({ key: key + '.' + _errs[i].key, value: _errs[i].value, message: _errs[i].message }); | ||
errs.push({ key: key + '.' + _key + '.' + _errs[i].key, value: _errs[i].value, message: _errs[i].message }); | ||
} | ||
} else if (format[key] instanceof this._formatTypes.Map) { | ||
for (_key in master[key]) { | ||
err = format[key].check(_key); | ||
if (err) { | ||
errs.push({ key: key, value: _key, message: err }); | ||
continue; | ||
} | ||
_errs = this._check(format[key].format, master[key][_key]); | ||
for (i = 0; i < _errs.length; i++) { | ||
errs.push({ key: key + '.' + _key + '.' + _errs[i].key, value: _errs[i].value, message: _errs[i].message }); | ||
} | ||
} | ||
} else if (typeof format[key] === typeof master[key] && Array.isArray(format[key]) === Array.isArray(master[key])) { | ||
_errs = this._check(format[key], master[key]); | ||
for (i = 0; i< _errs.length; i++) { | ||
errs.push({ key: key + '.' + _errs[i].key, value: _errs[i].value, message: _errs[i].message }); | ||
} | ||
} else if (typeof master[key] !== 'undefined') { | ||
errs.push({ key: key, value: JSON.stringify(master[key]), message: 'Format Error.' }); | ||
} | ||
} else if (typeof _format === typeof _master && Array.isArray(_format) === Array.isArray(_master)) { | ||
_errs = this._check(_format, _master, _parents); | ||
for (i = 0; i< _errs.length; i++) { | ||
errs.push({ key: key + '.' + _errs[i].key, value: _errs[i].value, message: _errs[i].message }); | ||
} | ||
} else if (typeof _master !== 'undefined') { | ||
errs.push({ key: key, value: JSON.stringify(_master), message: 'Format Error.' }); | ||
} | ||
@@ -319,0 +301,0 @@ } |
{ | ||
"name": "mastercheck", | ||
"version": "0.2.7", | ||
"version": "0.2.8", | ||
"description": "mastercheck", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -60,2 +60,4 @@ MasterCheck | ||
### Example Format Part Option | ||
Specify a function that returns an object or objects. | ||
Function have a parent in the arguments. | ||
#### MasterCheck.Number | ||
@@ -62,0 +64,0 @@ ``` |
@@ -14,3 +14,7 @@ var should = require('should'); | ||
type: masterCheck.format('String', { select: [ 'Dog', 'Cat' ] }), | ||
bool: masterCheck.format('Boolean') | ||
bool: masterCheck.format('Boolean', function(parents) { | ||
if (parents[0].parent && parents[0].parent.type === 'Dog') { | ||
return { required: true }; | ||
} | ||
}) | ||
}, | ||
@@ -41,3 +45,2 @@ list: [ | ||
code: 'test1_1', | ||
bool: true | ||
}, | ||
@@ -55,2 +58,3 @@ list: [ | ||
code: 'test2_1', | ||
type: 'Dog', | ||
bool: false | ||
@@ -195,3 +199,4 @@ }, | ||
obj: { | ||
type: 'Dog' | ||
type: 'Dog', | ||
bool: false | ||
}, | ||
@@ -198,0 +203,0 @@ arr: [ 1, 2 ] |
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
138
34398
638