Last active
March 22, 2017 07:30
-
-
Save qzm/740f8cf9467d899169cb6642e8654874 to your computer and use it in GitHub Desktop.
Validator 验证器,支持链式操作
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* file : validator | |
* author : qiuzhiming | |
* date : 2017-3-3 10:40:43 | |
* last : 2017-3-3 10:40:43 | |
* description : 用于表单的数据验证,支持链式操作,Submit的操作可以写在最后的Done里面 | |
* sample: v.test({ | |
* data : 'hello', | |
* rule : {Function} or {RegExp} or {Boolean} or {String} | |
* success: {Function}, 可以省略 | |
* fail: {Function},可以省略 | |
* }).test( | |
* // 其他需要测试功能,测试失败,后续都不会继续 | |
* ).done(function(){ | |
* // 提交数据等操作 | |
* }) | |
* 注意 :test 方法 和 each 方法不要混用 | |
*/ | |
'use strict' | |
define('Validator', [], function () { | |
// 内部函数,获取obj的数据类型 | |
var getType = function (obj) { | |
return Object.prototype.toString.call(obj).match(/\[object (\w+)\]/)[1]; | |
} | |
// 验证器 | |
var Validator = function () {}; | |
// 链式中断标志位 | |
Validator.prototype.break = false; | |
// 内建规则,均为Function类型 | |
Validator.prototype.rule = { | |
helloWorld: function (formData) { | |
return /helloWorld/.test(formData); | |
}, | |
required: function(formData){ | |
return formData !== ''; | |
} | |
}; | |
// each方法 ---> 不会中断的test方法 | |
Validator.prototype.each = function(option){ | |
this.test(option); | |
this.break = false; | |
return this; | |
}; | |
// test方法 | |
Validator.prototype.test = function (option) { | |
var _option = { | |
data: option.data, | |
rule: option.rule, | |
success: option.success, | |
fail: option.fail | |
} | |
// 未中断 | |
if (!this.break) { | |
// 获得结果 | |
var testResult, typeError = false, | |
ruleError = false; | |
switch (getType(_option.rule)) { | |
case 'Function': // 测试规则为Function类型的 | |
testResult = !!(_option.rule(_option.data)); | |
break; | |
case 'RegExp': // 测试规则为正则表达式类型的 | |
testResult = _option.rule.test(_option.data.toString()); | |
break; | |
case 'Boolean': // 如果直接是Boolean类型 | |
testResult = !!(_option.rule); | |
break; | |
case 'String': // 检查是否为内建的验证规则 | |
try { | |
testResult = this.rule[_option.rule](_option.data); | |
} catch (error) { | |
ruleError = true; | |
} | |
break; | |
default: | |
typeError = true; | |
} | |
if (testResult && !typeError && !ruleError) { // 验证成功 | |
if (getType(_option.success) === 'Function') { | |
_option.success.call(); | |
} | |
} else if (!testResult && !typeError && !ruleError) { // 验证失败 | |
if (getType(_option.fail) === 'Function') { | |
// 中断后面的验证 | |
this.break = true; | |
_option.fail.call(); | |
} | |
} else if (typeError) { // 类型错误 | |
this.break = true; | |
console.error('Validator: Test type is not Function or RegExp or String or Boolean!'); | |
} else if (ruleError) { // 规则错误 | |
this.break = true; | |
console.error('Validator: Build in rule not Found!') | |
} else { // 未知错误 | |
this.break = true; | |
console.error('Validator: Unknow error!') | |
} | |
} | |
// 支持链式调用 | |
return this; | |
}; | |
// 最后一步 | |
Validator.prototype.done = function (finalStep) { | |
if(!this.break){ | |
finalStep.call(); | |
} | |
}; | |
return new Validator() | |
}) | |
// // test | |
// var v = new Validator(); | |
// // 简单的使用例子 | |
// v.test({ | |
// data: 'hello', | |
// rule: 'helloWorld', | |
// success: function () { | |
// console.log('success'); | |
// }, | |
// fail: function () { | |
// console.log('fail'); | |
// } | |
// }).test({ | |
// data: 'hellWorld', | |
// rule: 'helloWorld', | |
// success: function () { | |
// console.log('success'); | |
// }, | |
// fail: function () { | |
// console.log('fail'); | |
// } | |
// }).test({ | |
// data: 'hello', | |
// rule: 'helloWorld', | |
// success: function () { | |
// console.log('success'); | |
// }, | |
// fail: function () { | |
// console.log('fail'); | |
// } | |
// }).done(function(){ | |
// console.log('Final Step') | |
// }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment