async-validate
Advanced tools
Comparing version 0.5.5 to 0.5.6
var plugin = require('zephyr') | ||
, format = require('format-util') | ||
, types = require('./types') | ||
, Reason = require('./reason'); | ||
@@ -118,36 +117,2 @@ | ||
/** | ||
* Rule for validating the type of a value. | ||
*/ | ||
function type() { | ||
var custom = [ | ||
'integer', 'float', 'array', 'regexp', 'object', 'method', 'null'] | ||
, rule = this.rule | ||
, value = this.value | ||
, type = rule.type | ||
, valid = true; | ||
// if value is required and value is undefined | ||
// no need to add this error message | ||
if(rule.required && (value === undefined)) { | ||
return; | ||
} | ||
if(~custom.indexOf(type)) { | ||
if(!types[type].call(this, value)) { | ||
valid = false; | ||
} | ||
// straight typeof check | ||
}else if(type && typeof(value) !== rule.type) { | ||
valid = false; | ||
} | ||
if(!valid) { | ||
this.raise( | ||
this.reasons.type, | ||
this.messages.types[type], this.field, rule.type); | ||
} | ||
} | ||
/** | ||
* Rule for validating minimum and maximum allowed values. | ||
@@ -248,3 +213,2 @@ */ | ||
Validator.prototype.range = range; | ||
Validator.prototype.type = type; | ||
Validator.prototype.format = format; | ||
@@ -251,0 +215,0 @@ Validator.prototype.isRoot = isRoot; |
@@ -17,3 +17,3 @@ /** | ||
null: '%s is not %s', | ||
method: '%s is not a %s (function)', | ||
method: '%s is not a %s', | ||
array: '%s is not an %s', | ||
@@ -20,0 +20,0 @@ object: '%s is not an %s', |
{ | ||
"name": "async-validate", | ||
"description": "Asynchronous validation for object properties.", | ||
"version": "0.5.5", | ||
"version": "0.5.6", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -32,3 +32,3 @@ "license": "MIT", | ||
"readme": "mdp --force -v", | ||
"browserify": "browserify -o async-validate.js -e ./lib/schema.js", | ||
"browserify": "browserify -o async-validate.js -e ./lib/schema.js && du -bh async-validate.js", | ||
"clean": "rm -rf coverage ./async-validate.js", | ||
@@ -35,0 +35,0 @@ "spec": "browserify -o test/spec.js -e test/index.js", |
@@ -9,3 +9,8 @@ /** | ||
this.required(); | ||
this.type(); | ||
if(!Array.isArray(this.value)) { | ||
this.raise( | ||
this.reasons.type, | ||
this.messages.types[this.rule.type], | ||
this.field, this.rule.type); | ||
} | ||
this.range(); | ||
@@ -12,0 +17,0 @@ } |
@@ -10,3 +10,10 @@ /** | ||
this.required(); | ||
this.type(); | ||
// straight typeof check | ||
if(typeof(this.value) !== this.rule.type) { | ||
this.raise( | ||
this.reasons.type, | ||
this.messages.types[this.rule.type], | ||
this.field, this.rule.type); | ||
} | ||
} | ||
@@ -13,0 +20,0 @@ cb(); |
@@ -31,2 +31,3 @@ var moment = require('moment'); | ||
&& this.source[this.field]); | ||
if(validate) { | ||
@@ -37,2 +38,3 @@ this.required(); | ||
} | ||
cb(); | ||
@@ -39,0 +41,0 @@ } |
@@ -10,6 +10,7 @@ /** | ||
var list = this.rule.list; | ||
if(list.indexOf(this.value) === -1) { | ||
if(!~list.indexOf(this.value)) { | ||
this.raise( | ||
this.reasons.enumerable, | ||
this.messages.enum, this.field, list.join(', ')); | ||
this.messages.enum, | ||
this.field, list.join(', ')); | ||
} | ||
@@ -16,0 +17,0 @@ } |
@@ -9,3 +9,11 @@ /** | ||
this.required(); | ||
this.type(); | ||
if(typeof(this.value) !== 'number' | ||
|| Number(this.value) === parseInt(this.value)) { | ||
this.raise( | ||
this.reasons.type, | ||
this.messages.types[this.rule.type], | ||
this.field, this.rule.type); | ||
} | ||
this.range(); | ||
@@ -12,0 +20,0 @@ } |
@@ -9,3 +9,11 @@ /** | ||
this.required(); | ||
this.type(); | ||
if(typeof(this.value) !== 'number' | ||
|| Number(this.value) !== parseInt(this.value)) { | ||
this.raise( | ||
this.reasons.type, | ||
this.messages.types[this.rule.type], | ||
this.field, this.rule.type); | ||
} | ||
this.range(); | ||
@@ -12,0 +20,0 @@ } |
@@ -9,3 +9,8 @@ /** | ||
this.required(); | ||
this.type(); | ||
if(typeof this.value !== 'function') { | ||
this.raise( | ||
this.reasons.type, | ||
this.messages.types[this.rule.type], | ||
this.field, this.rule.type); | ||
} | ||
} | ||
@@ -12,0 +17,0 @@ cb(); |
@@ -5,3 +5,8 @@ module.exports = function() { | ||
this.required(); | ||
this.type(); | ||
if(this.value !== null) { | ||
this.raise( | ||
this.reasons.type, | ||
this.messages.types[this.rule.type], | ||
this.field, this.rule.type); | ||
} | ||
} | ||
@@ -8,0 +13,0 @@ cb(); |
@@ -9,3 +9,9 @@ /** | ||
this.required(); | ||
this.type(); | ||
// straight typeof check | ||
if(typeof(this.value) !== this.rule.type) { | ||
this.raise( | ||
this.reasons.type, | ||
this.messages.types[this.rule.type], | ||
this.field, this.rule.type); | ||
} | ||
this.range(); | ||
@@ -12,0 +18,0 @@ } |
@@ -0,1 +1,8 @@ | ||
function isObject(value, Type) { | ||
if(typeof Type === 'function') { | ||
return (value instanceof Type); | ||
} | ||
return typeof(value) === 'object' && !Array.isArray(value); | ||
} | ||
/** | ||
@@ -11,4 +18,10 @@ * Validates an object. | ||
this.required(); | ||
this.type(); | ||
if(!isObject(this.value, this.rule.Type)) { | ||
this.raise( | ||
this.reasons.type, | ||
this.messages.types[this.rule.type], | ||
this.field, this.rule.type); | ||
} | ||
// nested deep properties | ||
@@ -15,0 +28,0 @@ if(this.rule.additional === false) { |
@@ -0,1 +1,13 @@ | ||
function isRegExp(value) { | ||
if(value instanceof RegExp) { | ||
return true; | ||
} | ||
try { | ||
new RegExp(value); | ||
return true; | ||
}catch(e) {} | ||
return false; | ||
} | ||
/** | ||
@@ -9,3 +21,7 @@ * Validates the regular expression type. | ||
this.required(); | ||
this.type(); | ||
if(!isRegExp(this.value)) { | ||
this.raise( | ||
this.reasons.type, | ||
this.messages.types[this.rule.type], this.field, this.rule.type); | ||
} | ||
} | ||
@@ -12,0 +28,0 @@ cb(); |
@@ -9,3 +9,16 @@ /** | ||
this.required(); | ||
this.type(); | ||
// if value is required and value is undefined | ||
// no need to add this error message | ||
if(this.rule.required && this.value === undefined) { | ||
return cb(); | ||
} | ||
if(typeof this.value !== 'string') { | ||
this.raise( | ||
this.reasons.type, | ||
this.messages.types[this.rule.type], | ||
this.field, this.rule.type); | ||
} | ||
this.range(); | ||
@@ -12,0 +25,0 @@ this.pattern(); |
@@ -13,3 +13,3 @@ var assert = require('chai').assert; | ||
assert.equal(errors.length, 1); | ||
assert.equal(errors[0].message, "mock is not a method (function)"); | ||
assert.equal(errors[0].message, "mock is not a method"); | ||
done(); | ||
@@ -16,0 +16,0 @@ }); |
@@ -22,3 +22,3 @@ var assert = require('chai').assert; | ||
validator.validate({}, function(errors, fields) { | ||
assert.equal(errors.length, 1); | ||
assert.equal(errors.length, 2); | ||
assert.equal(errors[0].message, "address is required"); | ||
@@ -25,0 +25,0 @@ done(); |
126364
2733
73