工作所需进行工具封装,在此记录
import {
isObject,
isFunction,
isString,
isNumber,
isArray,
isBoolean,
} from '../xxx';
class Validator {
constructor(data, rules) {
this._rules = rules;
this.data = data;
}
_typeMap = {
'Any': 'Any',
'[object Function]': 'Function',
'[object Object]': 'Object',
'[object Array]': 'Array',
'[object Boolean]': 'Boolean',
'[object Number]': 'Number',
'[object String]': 'String',
}
getType(data = this.data) {
const acceptOriginType = Object.prototype.toString.call(data)
return this._typeMap[acceptOriginType];
}
normalValidate(data = this.data, rules = this._rules) {
const dataType = this.getType(data);
if (dataType !== 'Object') {
this._errorHandler(`应为Object,但接收到${dataType}`)
}
const source = {
keyList: Object.keys(data),
valueList: Object.values,
};
Object.entries(rules).map(([filedName, filedRule]) => {
const {
type = 'Any',
require = false,
range,
childNodes,
reg,
} = filedRule;
if (require && !source.keyList.includes(filedName)) {
this._errorHandler(`${filedName}为必传参数,当前参数${source.keyList}`)
}
const sourceValue = data[filedName];
const sourceValueType = this.getType(sourceValue);
if (sourceValue !== undefined && sourceValue !== 'undefined') {
if (type !== sourceValueType && type !== 'Any') {
this._errorHandler(`${filedName}参数应为${type},但接收到${sourceValue}=> ${sourceValueType}`)
}
const r = new RegExp(reg);
if (reg && !r.test(`${sourceValue}`)) {
this._errorHandler(`${filedName}参数应符合${reg}规则,但接收到${sourceValue}=> ${sourceValueType}`)
}
if (range && range.length) {
const rangeLimit = range.some(item => !['String', 'Number'].includes(this.getType(item)))
if (rangeLimit) this._errorHandler('range只接受String/Number')
if (!range.includes(sourceValue)) this._errorHandler(`${filedName}的值只能在${range}中选择`)
}
const childNodesType = this.getType(childNodes);
if (childNodes && childNodesType !== 'Object') {
this._errorHandler(`childNodes应为Object,但接收到${childNodesType}`)
}
if (childNodes) {
this.normalValidate(sourceValue, childNodes)
}
}
})
return this;
}
validateObject(data = this.data) {
const result = isObject(data);
if (!result) this._errorHandler(`参数错误,应为Object,但接收到${this.getType(data)}`);
return this;
}
validateFunction(data = this.data) {
const result = isFunction(data);
if (!result) this._errorHandler(`参数错误,应为Function,但接收到${this.getType(data)}`);
return this;
}
validateString(data = this.data) {
const result = isString(data);
if (!result) this._errorHandler(`参数错误,应为String,但接收到${this.getType(data)}`);
return this;
}
validateNumber(data = this.data) {
const result = isNumber(data)
if (!result) this._errorHandler(`参数错误,应为Number,但接收到${this.getType(data)}`);
return this;
}
validateArray(data = this.data) {
const result = isArray(data);
if (!result) this._errorHandler(`参数错误,应为Array,但接收到${this.getType(data)}`);
return this;
}
validateBoolean(data = this.data) {
const result = isBoolean(data);
if (!result) this._errorHandler(`参数错误,应为Boolean,但接收到${this.getType(data)}`);
return this;
}
_dataToString(data) {
const type = this.getType(data)
return type === 'Function' ? data : JSON.stringify(data)
}
_errorHandler(msg) {
throw new Error(`参数错误!${msg}`)
}
}
export default Validator;