Skip to content
This repository has been archived by the owner on Apr 12, 2021. It is now read-only.

Commit

Permalink
clean validator interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ppoffice committed Jul 13, 2015
1 parent 91e44a0 commit f2747a8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var rules = {
|between:min,max|The field under validation must have a size between the given min and max.Strings and numerics are evaluated.|
|boolean |The field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1", "0", '1' and '-'.|
|date |The field under validation must be a valid date according to the Date.parse function.|
|date_format:format|The field under validation must match the format defined according to the Validator.utils.dateFormat function.|
|date_format:format|The field under validation must match the format defined according to the meizz's dateFormat function.|
|different:field|The given field must be different than the field under validation.|
|digits:value|The field under validation must be numeric and must have an exact length of value.|
|digits_between:min,max|The field under validation must have a length between the given min and max.|
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var rules = {
|between:min,max|验证域的值必须在min和max之间,验证域可以是数字或者字符串。|
|boolean |验证域的值可以看作是布尔值,可以是true,false,1,0,"1","0",'1' and '0'。|
|date |验证域必须为日期字符串形式,可以被Date.parse方法解析。|
|date_format:format|验证域必须符合制定的日期格式,允许的日期格式参照源代码中的Validator.utils.dateFormat方法|
|date_format:format|验证域必须符合制定的日期格式,允许的日期格式参照源代码中的dateFormat方法|
|different:field|验证域的值必须域指定域的值不同。|
|digits:value|验证域必须为数字,且其位数为给定的位数。|
|digits_between:min,max|验证域必须为数字,且其位数在min和max之间。|
Expand Down
89 changes: 41 additions & 48 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,30 @@
'use strict';

// Singleton
var Validator = function () {
if(!this.instance)
this.instance = this;
return this.instance;
var Validator = function () {};

var instance = instance | new Validator,
__object = null;

// Validation failed at @field on @rule.
var fail = function (field, rule) {
return {
status: 'failed',
field: field,
rule: rule,
};
};

// Validation succeeded.
var success = function () {
return true;
};

Validator.prototype.requires = {

// The field under validation must be present in the input data.
'required': function (field) {
return !!this.object[field];
return !!__object[field];
},

// required_if:field,value,...
Expand All @@ -31,7 +44,7 @@
for (var i = 1; i < arguments.length; i = i + 2) {
var _field = arguments[i],
_value = arguments[i + 1];
if(!this.object[_field] || this.object[_field] != _value)
if(!__object[_field] || __object[_field] != _value)
return true;
}
return this.requires.required.call(this, field);
Expand All @@ -44,7 +57,7 @@
return false;
var field = arguments[0];
for (var i = 1; i < arguments.length; i++) {
if(arguments[i] in this.object)
if(arguments[i] in __object)
return this.requires.required.call(this, field);
}
return true;
Expand All @@ -57,7 +70,7 @@
return false;
var field = arguments[0];
for (var i = 1; i < arguments.length; i++) {
if(!(arguments[i] in this.object))
if(!(arguments[i] in __object))
return true;
}
return this.requires.required.call(this, field);
Expand All @@ -70,7 +83,7 @@
return false;
var field = arguments[0];
for (var i = 1; i < arguments.length; i++) {
if(!(arguments[i] in this.object))
if(!(arguments[i] in __object))
return this.requires.required.call(this, field);
}
return true;
Expand All @@ -83,7 +96,7 @@
return false;
var field = arguments[0];
for (var i = 1; i < arguments.length; i++) {
if(arguments[i] in this.object)
if(arguments[i] in __object)
return true;
}
return this.requires.required.call(this, field);
Expand Down Expand Up @@ -161,17 +174,17 @@
},

// date_format:format
// The field under validation must match the format defined according to the Validator.utils.dateFormat function.
// The field under validation must match the format defined according to the Utils.dateFormat function.
'date_format': function (value, format) {
var date = new Date(Date.parse(value)),
dateString = this.utils.dateFormat(date, format);
dateString = Utils.dateFormat(date, format);
return value === dateString;
},

// different:field
// The given field must be different than the field under validation.
'different': function (value, field) {
return value != this.object[field];
return value != __object[field];
},

// digits:value
Expand All @@ -196,7 +209,7 @@
'in': function () {
if(arguments.length <= 1)
return false;
return this.utils.inArray(Array.prototype.slice.call(arguments, 1), arguments[0]);
return Utils.inArray(Array.prototype.slice.call(arguments, 1), arguments[0]);
},

// The field under validation must have an integer value.
Expand Down Expand Up @@ -285,7 +298,7 @@
// same:field
// The given field must match the field under validation.
'same': function (value, field) {
return value === this.object[field];
return value === __object[field];
},

// size:value
Expand Down Expand Up @@ -319,7 +332,7 @@

};

Validator.prototype.utils = {
var Utils = {

// author: meizz
// Stringify date to defined formats.
Expand Down Expand Up @@ -374,22 +387,6 @@

};

// Validation failed at @field on @rule.
Validator.prototype.fail = function (field, rule) {
delete this.object;
return {
status: 'failed',
field: field,
rule: rule,
};
};

// Validation succeeded.
Validator.prototype.success = function () {
delete this.object;
return true;
};

// Add a validator.
// @fn must be a Function or an RegExp object.
// if @fn is a Function, it must receive value as its first argument, and return true if validation succeeds.
Expand All @@ -401,34 +398,30 @@
};

Validator.prototype.validate = function(object, _rules) {
this.object = object;
__object = object;
for(var field in _rules) {
var ruleString = _rules[field],
rules = this.utils.parser(ruleString);
rules = Utils.parser(ruleString);
for (var i = 0; i < rules.length; i++) {
var key = rules[i].key,
value = rules[i].value;
// Match require rules.
if(key in this.requires) {
if(key in this.requires) { // Match require rules.
value.unshift(field);
if(!this.requires[key].apply(this, value))
return this.fail(field, key);
} else if (field in this.object && key in this.validators) {
// Match other rules.
// Validator is a Function.
if(typeof this.validators[key] === 'function') {
value.unshift(this.object[field]);
return fail(field, key);
} else if (field in __object && key in this.validators) { // Match other rules.
if(typeof this.validators[key] === 'function') { // Validator is a Function.
value.unshift(__object[field]);
if(!this.validators[key].apply(this, value))
return this.fail(field, key);
} else if (this.validators[key].constructor.name === 'RegExp') {
// Validator is an RegExp.
if(!this.validators[key].test(this.object[field]))
return this.fail(field, key);
return fail(field, key);
} else if (this.validators[key].constructor.name === 'RegExp') { // Validator is an RegExp.
if(!this.validators[key].test(__object[field]))
return fail(field, key);
}
}
}
}
return this.success();
return success();
};

if (typeof(require) !== 'undefined' && typeof(module) !== 'undefined' &&
Expand Down

0 comments on commit f2747a8

Please sign in to comment.