async-validate
Advanced tools
Comparing version 0.4.0 to 0.4.1
var plugin = require('zephyr') | ||
, format = require('./format') | ||
, types = require('./type-test') | ||
, ValidationError = require('./error'); | ||
, ValidationError = require('./error') | ||
, reasons = { | ||
type: 'type', | ||
required: 'required', | ||
pattern: 'pattern', | ||
length: 'length', | ||
enumerable: 'enum', | ||
date: 'date', | ||
whitespace: 'whitespace', | ||
min: 'min', | ||
max: 'max', | ||
} | ||
@@ -16,5 +27,17 @@ /** | ||
} | ||
// reason constants | ||
this.reasons = reasons; | ||
} | ||
/** | ||
* Get or set a reason id for the current error. | ||
*/ | ||
function reason(id) { | ||
if(id) { | ||
this._reason = id; | ||
} | ||
return this._reason; | ||
} | ||
/** | ||
* Helper for creating validation errors. | ||
@@ -31,2 +54,8 @@ * | ||
if(typeof this.rule.message === 'object' | ||
&& this._reason | ||
&& this.rule.message[this._reason]) { | ||
msg = this.rule.message[this._reason]; | ||
} | ||
if(arguments.length > 1 && !this.rule.message) { | ||
@@ -38,2 +67,5 @@ var args = Array.prototype.slice.call(arguments, 1); | ||
err.field = this.field; | ||
if(this._reason) { | ||
err.reason = this._reason; | ||
} | ||
return err; | ||
@@ -61,2 +93,3 @@ } | ||
|| this.value === undefined || this.value === null)) { | ||
this.reason(this.reasons.required); | ||
this.raise(this.messages.required, this.field); | ||
@@ -71,2 +104,3 @@ } | ||
if(/^\s+$/.test(this.value) || this.value === '') { | ||
this.reason(this.reasons.whitespace); | ||
this.raise(this.messages.whitespace, this.field); | ||
@@ -82,2 +116,3 @@ } | ||
if(list.indexOf(this.value) === -1) { | ||
this.reason(this.reasons.enumerable); | ||
this.raise(this.messages.enum, this.field, list.join(', ')); | ||
@@ -93,2 +128,3 @@ } | ||
if(!this.rule.pattern.test(this.value)) { | ||
this.reason(this.reasons.pattern); | ||
this.raise(this.messages.pattern.mismatch, | ||
@@ -132,8 +168,12 @@ this.field, this.value, this.rule.pattern); | ||
if(len && (val !== rule.len)) { | ||
this.reason(this.reasons.length); | ||
this.raise(this.messages[key].len, this.field, rule.len); | ||
}else if( min && !max && val < rule.min ) { | ||
}else if(min && !max && val < rule.min ) { | ||
this.reason(this.reasons.min); | ||
this.raise(this.messages[key].min, this.field, rule.min); | ||
}else if( max && !min && val > rule.max ) { | ||
this.reason(this.reasons.max); | ||
this.raise(this.messages[key].max, this.field, rule.max); | ||
}else if(min && max && (val < rule.min || val > rule.max) ) { | ||
this.reason(val < rule.min ? this.reasons.min : this.reasons.max); | ||
this.raise(this.messages[key].range, this.field, rule.min, rule.max); | ||
@@ -160,2 +200,3 @@ } | ||
if(!types[type](value)) { | ||
this.reason(this.reasons.type); | ||
this.raise(this.messages.types[type], rule.field, rule.type); | ||
@@ -165,2 +206,3 @@ } | ||
}else if(type && typeof(value) !== rule.type) { | ||
this.reason(this.reasons.type); | ||
this.raise(this.messages.types[type], rule.field, rule.type); | ||
@@ -183,3 +225,3 @@ } | ||
Validator.prototype.raise = raise; | ||
Validator.prototype.reason = reason; | ||
Validator.prototype.required = required; | ||
@@ -186,0 +228,0 @@ Validator.prototype.shouldValidate = shouldValidate; |
{ | ||
"name": "async-validate", | ||
"description": "Asynchronous validation for object properties.", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -14,2 +14,3 @@ var expect = require('chai').expect | ||
var v = Validator.Type({field: 'mock', rule: {}}); | ||
expect(v.reason()).to.eql(undefined); | ||
// trigger default message code path | ||
@@ -16,0 +17,0 @@ v.error(); |
103277
66
2264