Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function parseJSON(str) {
if (!str)
return null;
var NOTICE = 'Invalid option object "' + str + '".';
// remove braces
str = str.slice(1, -1);
var result = {};
var arr = str.split(',');
$.each(arr, function (i, v) {
arr[i] = $.trim(v);
if (!arr[i])
throw new Error(NOTICE);
var arr2 = arr[i].split(':');
var key = $.trim(arr2[0]),
value = $.trim(arr2[1]);
if (!key || !value)
throw new Error(NOTICE);
result[getValue(key)] = $.trim(getValue(value));
});
// 'abc' -> 'abc' '"abc"' -> 'abc'
function Rule(name, oper) {
var self = this;
self.name = name;
if (oper instanceof RegExp) {
self.operator = function (opts, commit) {
var rslt = oper.test($(opts.element).val());
commit(rslt ? null : opts.rule, _getMsg(opts, rslt));
};
} else if ($.isFunction(oper)) {
self.operator = function (opts, commit) {
var rslt = oper.call(this, opts, function (result, msg) {
commit(result ? null : opts.rule, msg || _getMsg(opts, result));
});
// 当是异步判断时, 返回 undefined, 则执行上面的 commit
if (rslt !== undefined) {
commit(rslt ? null : opts.rule, _getMsg(opts, rslt));
}
};
} else {
throw new Error('The second argument must be a regexp or a function.');
}
}
truly = !!ele.val();
}
// 非必要且没有值的时候, 直接 callback
if (!truly) {
callback && callback(null, null);
return;
}
}
if (!$.isArray(rules))
throw new Error('No validation rule specified or not specified as an array.');
var tasks = [];
$.each(rules, function (i, item) {
var obj = utils.parseRule(item),
ruleName = obj.name,
param = obj.param;
var ruleOperator = Rule.getOperator(ruleName);
if (!ruleOperator)
throw new Error('Validation rule with name "' + ruleName + '" cannot be found.');
var options = getMsgOptions(param, ruleName, self);
tasks.push(function (cb) {
// cb 为 async.series 每个 tasks 函数 的 callback!!
// callback(err, results)
// self._validator 为当前 Item 对象所在的 Validator 对象
ruleOperator.call(self._validator, options, cb);
});
function _getMsg(opts, b) {
var ruleName = opts.rule;
var msgtpl;
if (opts.message) { // user specifies a message
if ($.isPlainObject(opts.message)) {
msgtpl = opts.message[b ? 'success' : 'failure'];
// if user's message is undefined,use default
typeof msgtpl === 'undefined' && (msgtpl = messages[ruleName][b ? 'success' : 'failure']);
} else {//just string
msgtpl = b ? '' : opts.message
}
} else { // use default
msgtpl = messages[ruleName][b ? 'success' : 'failure'];
}
return msgtpl ? compileTpl(opts, msgtpl) : msgtpl;
}
getter: function(val){
val = $.trim(val);
// 在获取的时候动态判断是否required,来追加或者删除 rule: required
if (this.get('required')) {
if (!val || !hasRequired(val)) {
val = $.trim('required ' + val);
}
} else {
if (hasRequired(val)) {
val = $.trim((' ' + val + ' ').replace(' required ', ' '));
}
}
return val;
}
},
getter: function(val){
val = $.trim(val);
// 在获取的时候动态判断是否required,来追加或者删除 rule: required
if (this.get('required')) {
if (!val || !hasRequired(val)) {
val = $.trim('required ' + val);
}
} else {
if (hasRequired(val)) {
val = $.trim((' ' + val + ' ').replace(' required ', ' '));
}
}
return val;
}
},
getter: function(val){
val = $.trim(val);
// 在获取的时候动态判断是否required,来追加或者删除 rule: required
if (this.get('required')) {
if (!val || !hasRequired(val)) {
val = $.trim('required ' + val);
}
} else {
if (hasRequired(val)) {
val = $.trim((' ' + val + ' ').replace(' required ', ' '));
}
}
return val;
}
},
}
});
truly = checked;
break;
default:
truly = !!ele.val();
}
// 非必要且没有值的时候, 直接 callback
if (!truly) {
callback && callback(null, null);
return;
}
}
if (!$.isArray(rules))
throw new Error('No validation rule specified or not specified as an array.');
var tasks = [];
$.each(rules, function (i, item) {
var obj = utils.parseRule(item),
ruleName = obj.name,
param = obj.param;
var ruleOperator = Rule.getOperator(ruleName);
if (!ruleOperator)
throw new Error('Validation rule with name "' + ruleName + '" cannot be found.');
var options = getMsgOptions(param, ruleName, self);
tasks.push(function (cb) {
$.each(arr, function (i, v) {
arr[i] = $.trim(v);
if (!arr[i])
throw new Error(NOTICE);
var arr2 = arr[i].split(':');
var key = $.trim(arr2[0]),
value = $.trim(arr2[1]);
if (!key || !value)
throw new Error(NOTICE);
result[getValue(key)] = $.trim(getValue(value));
});
$.each(arr, function (i, v) {
arr[i] = $.trim(v);
if (!arr[i])
throw new Error(NOTICE);
var arr2 = arr[i].split(':');
var key = $.trim(arr2[0]),
value = $.trim(arr2[1]);
if (!key || !value)
throw new Error(NOTICE);
result[getValue(key)] = $.trim(getValue(value));
});