...
|
...
|
@@ -34,11 +34,11 @@ const _numberValid = (option) => { |
|
|
const value = option.value;
|
|
|
|
|
|
// 设置默认值
|
|
|
option.require = option.require === undefined ? false : option.require;
|
|
|
option.integer = option.integer === undefined ? false : option.integer;
|
|
|
option.empty = option.empty === undefined ? true : option.empty;
|
|
|
option.require = option.require === void(0) ? false : option.require;
|
|
|
option.integer = option.integer === void(0) ? false : option.integer;
|
|
|
option.empty = option.empty === void(0) ? true : option.empty;
|
|
|
|
|
|
if (value === undefined) {
|
|
|
if (value === void(0)) {
|
|
|
if (option.require === true) {
|
|
|
throw _errJson(`${option.param} 参数是必须的`);
|
|
|
} else {
|
...
|
...
|
@@ -55,20 +55,20 @@ const _numberValid = (option) => { |
|
|
if (option.empty === false) {
|
|
|
throw _errJson(`${option.param} 参数不能为空字符串`);
|
|
|
} else {
|
|
|
return undefined;
|
|
|
return void(0);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
const numValue = Number(value);
|
|
|
|
|
|
if (!Number.isNaN(numValue) && Math.abs(numValue) !== Infinity) {
|
|
|
if (option.equal !== undefined) {
|
|
|
if (option.equal !== void(0)) {
|
|
|
if (numValue !== option.equal) {
|
|
|
throw _errJson(`${option.param} 参数必须等于 ${option.equal}`);
|
|
|
}
|
|
|
return numValue;
|
|
|
}
|
|
|
if (option.equalArr !== undefined) {
|
|
|
if (option.equalArr !== void(0)) {
|
|
|
let result = false;
|
|
|
|
|
|
for (let item of option.equalArr) {
|
...
|
...
|
@@ -89,12 +89,12 @@ const _numberValid = (option) => { |
|
|
throw _errJson(`${option.param} 参数必须是整数`);
|
|
|
}
|
|
|
}
|
|
|
if (option.smaller !== undefined) {
|
|
|
if (option.smaller !== void(0)) {
|
|
|
if (!numValue < option.smaller) {
|
|
|
throw _errJson(`${option.param} 参数必须小于 ${option.smaller}`);
|
|
|
}
|
|
|
}
|
|
|
if (option.bigger !== undefined) {
|
|
|
if (option.bigger !== void(0)) {
|
|
|
if (!numValue > option.bigger) {
|
|
|
throw _errJson(`${option.param} 参数必须大于 ${option.bigger}`);
|
|
|
}
|
...
|
...
|
@@ -124,7 +124,7 @@ const _stringValid = (option) => { |
|
|
option.require = option.require || false;
|
|
|
option.empty = option.empty || true;
|
|
|
|
|
|
if (value === undefined) {
|
|
|
if (value === void(0)) {
|
|
|
if (option.require === true) {
|
|
|
throw _errJson(`${option.param} 参数是必须的`);
|
|
|
} else {
|
...
|
...
|
@@ -144,12 +144,12 @@ const _stringValid = (option) => { |
|
|
throw _errJson(`${option.param} 参数不能为空`);
|
|
|
} else {
|
|
|
if (option.emptyFilter === true) {
|
|
|
return undefined;
|
|
|
return void(0);
|
|
|
}
|
|
|
return strValue;
|
|
|
}
|
|
|
}
|
|
|
if (option.regex !== undefined) {
|
|
|
if (option.regex !== void(0)) {
|
|
|
switch (option.regex) {
|
|
|
case 'phone':
|
|
|
if (!/^1[0-9]{10}$/.test(strValue)) {
|
...
|
...
|
@@ -168,12 +168,12 @@ const _stringValid = (option) => { |
|
|
}
|
|
|
return strValue;
|
|
|
}
|
|
|
if (option.end !== undefined) {
|
|
|
if (option.end !== void(0)) {
|
|
|
if (!strValue.length < option.end) {
|
|
|
throw _errJson(`${option.param} 参数长度必须小于 ${option.end}`);
|
|
|
}
|
|
|
}
|
|
|
if (option.start !== undefined) {
|
|
|
if (option.start !== void(0)) {
|
|
|
if (!strValue.length > option.start) {
|
|
|
throw _errJson(`${option.param} 参数长度必须大于 ${option.start}`);
|
|
|
}
|
...
|
...
|
@@ -197,7 +197,7 @@ const _booleanValid = (option) => { |
|
|
option.require = option.require || false;
|
|
|
option.empty = option.empty || true;
|
|
|
|
|
|
if (value === undefined) {
|
|
|
if (value === void(0)) {
|
|
|
if (option.require === true) {
|
|
|
throw _errJson(`${option.param} 参数是必须的`);
|
|
|
} else {
|
...
|
...
|
@@ -210,7 +210,7 @@ const _booleanValid = (option) => { |
|
|
throw _errJson(`${option.param} 参数不能为空`);
|
|
|
} else {
|
|
|
if (option.emptyFilter === true) {
|
|
|
return undefined;
|
|
|
return void(0);
|
|
|
}
|
|
|
return value;
|
|
|
}
|
...
|
...
|
@@ -229,7 +229,7 @@ const _objectValid = (option) => { |
|
|
option.require = option.require || false;
|
|
|
option.empty = option.empty || true;
|
|
|
|
|
|
if (value === undefined || value === null) {
|
|
|
if (value === void(0) || value === null) {
|
|
|
if (option.require === true) {
|
|
|
throw _errJson(`${option.param} 参数是必须的`);
|
|
|
} else {
|
...
|
...
|
@@ -268,25 +268,25 @@ const valid = (data, option) => { |
|
|
switch (opt.type) {
|
|
|
case 'number':
|
|
|
data[item] = _numberValid(opt);
|
|
|
if (data[item] === undefined) {
|
|
|
if (data[item] === void(0)) {
|
|
|
delete data[item];
|
|
|
}
|
|
|
break;
|
|
|
case 'string':
|
|
|
data[item] = _stringValid(opt);
|
|
|
if (data[item] === undefined) {
|
|
|
if (data[item] === void(0)) {
|
|
|
delete data[item];
|
|
|
}
|
|
|
break;
|
|
|
case 'boolean':
|
|
|
data[item] = _booleanValid(opt);
|
|
|
if (data[item] === undefined) {
|
|
|
if (data[item] === void(0)) {
|
|
|
delete data[item];
|
|
|
}
|
|
|
break;
|
|
|
case 'object':
|
|
|
data[item] = _objectValid(opt);
|
|
|
if (data[item] === undefined) {
|
|
|
if (data[item] === void(0)) {
|
|
|
delete data[item];
|
|
|
}
|
|
|
break;
|
...
|
...
|
|