toolmanage
Version:
'smart基础工具类npm(beast)'
146 lines (143 loc) • 4.43 kB
JavaScript
//获取键值
function getItsVal(object, key) {
if (!key)
return null;
var val = null;
//key 为函数
if (key && typeof (key) == "function") {
val = key(object);
//key 不为函数
} else {
val = getPropertyValue(object, key);
};
return val;
}
//key可能形如'itsExtData.cpc.city.val'
function getPropertyValue(object, key) {
try {
var keys = key.split('.');
if (keys.length == 1) {
return object[key];
}
var nextKeys = key.substring(key.indexOf('.') + 1);
var nextObject = object[keys[0]];
return getPropertyValue(nextObject, nextKeys);
} catch (e) {
console.log(key);
console.log(e);
return null;
}
}
// 判断数据是否为对象
function isObject(o) {
var gettype = Object.prototype.toString
return gettype.call(o) === '[object Object]'
}
// 判断数据是否为数组
function isArray(o) {
var gettype = Object.prototype.toString
return gettype.call(o) === '[object Array]'
}
//判断数据是否为空
function isEmpty(val) {
//对象
if (isObject(val)) {
return Object.keys(val).length === 0
}
// 数组
if(isArray(val)) {
return val.length === 0
}
return !val;
}
//正则匹配,regExp:需要传入相应正则规则
function isRegMatch(val, regExp) {
var pattern = new RegExp(regExp, 'im');
return !pattern.test(val);
}
//时间转换为毫秒
function dateTransform(date){
var curdate = new Date(typeof (date) == "string" ? date.replace(/-/g, '/') : date);
return curdate.getTime()
}
//model 验证对象,validatorConfig为配置的验证条件
//数据结构
// var model={
// isUsed:true,
// telphone:'18782924967'
// }
// var validatorConfig={
// required:true,
// preCondition:function(model){
// return model.isUsed
// },
// description:'手机号码不能为空',
// regExp:'/^1[34578]\d{9}$/',
// regExpMsg:'手机号码格式不正确'
// }
var validate = function (model, validatorConfig) {
for (var key in validatorConfig) {
var rule = validatorConfig[key];
var description = typeof (rule.description) == "function" ? rule.description(model) : rule.description;
var preCondition = rule.preCondition;
var val = getItsVal(model, rule.getVal || key)
//required:是否必填项 preCondition:预置条件
if (
(preCondition == undefined || (preCondition && preCondition(model)))
&& rule.required && isEmpty(val)) {
return {
status:false,
msg:rule.description
};
}
//regExp
if (
(preCondition == undefined || (preCondition && preCondition(model)))
&& rule.regExp && isRegMatch(val, rule.regExp)) {
return {
status:false,
msg:rule.regExpMsg
}
}
//min,开始时间不能大于结束时间
if (
(preCondition == undefined || (preCondition && preCondition(model)))
&& rule.min) {
var minVal = getItsVal(model, rule.min);
if (dateTransform(val) < dateTransform(minVal)) {
return {
status:false,
msg:rule.minMsg
}
}
}
// after
if (
(preCondition == undefined || (preCondition && preCondition(model))) &&
rule.after) {
var gtVal = getItsVal(model, rule.after);
if (dateTransform(val) < dateTransform(gtVal)) {
return {
status:false,
msg:rule.afterMsg
}
}
}
// before
if (
(preCondition == undefined || (preCondition && preCondition(model))) &&
rule.before) {
var gtVal = getItsVal(model, rule.before);
if (dateTransform(val) > dateTransform(gtVal)) {
return {
status:false,
msg:rule.beforeMsg
}
}
}
}
return {
status:true
};
}
module.exports=validate