feat: 初始化
This commit is contained in:
420
uni_modules/uni-ui/components/uni-forms/uni-forms.vue
Normal file
420
uni_modules/uni-ui/components/uni-forms/uni-forms.vue
Normal file
@ -0,0 +1,420 @@
|
||||
<template>
|
||||
<!-- -->
|
||||
<view class="uni-forms" :class="{'uni-forms--top':!border}">
|
||||
<form @submit.stop="submitForm" @reset="resetForm">
|
||||
<slot></slot>
|
||||
</form>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
/**
|
||||
* Forms 表单
|
||||
* @description 由输入框、选择器、单选框、多选框等控件组成,用以收集、校验、提交数据
|
||||
* @tutorial https://ext.dcloud.net.cn/plugin?id=2773
|
||||
* @property {Object} rules 表单校验规则
|
||||
* @property {String} validateTrigger = [bind|submit] 校验触发器方式 默认 submit 可选
|
||||
* @value bind 发生变化时触发
|
||||
* @value submit 提交时触发
|
||||
* @property {String} labelPosition = [top|left] label 位置 默认 left 可选
|
||||
* @value top 顶部显示 label
|
||||
* @value left 左侧显示 label
|
||||
* @property {String} labelWidth label 宽度,默认 65px
|
||||
* @property {String} labelAlign = [left|center|right] label 居中方式 默认 left 可选
|
||||
* @value left label 左侧显示
|
||||
* @value center label 居中
|
||||
* @value right label 右侧对齐
|
||||
* @property {String} errShowType = [undertext|toast|modal] 校验错误信息提示方式
|
||||
* @value undertext 错误信息在底部显示
|
||||
* @value toast 错误信息toast显示
|
||||
* @value modal 错误信息modal显示
|
||||
* @event {Function} submit 提交时触发
|
||||
*/
|
||||
import Vue from 'vue'
|
||||
Vue.prototype.binddata = function(name, value, formName) {
|
||||
if (formName) {
|
||||
this.$refs[formName].setValue(name, value)
|
||||
} else {
|
||||
let formVm
|
||||
for (let i in this.$refs) {
|
||||
const vm = this.$refs[i]
|
||||
if (vm && vm.$options && vm.$options.name === 'uniForms') {
|
||||
formVm = vm
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!formVm) return console.error('当前 uni-froms 组件缺少 ref 属性')
|
||||
formVm.setValue(name, value)
|
||||
}
|
||||
}
|
||||
|
||||
import Validator from './validate.js'
|
||||
|
||||
export default {
|
||||
name: 'uniForms',
|
||||
props: {
|
||||
value: {
|
||||
type: Object,
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
},
|
||||
// 表单校验规则
|
||||
rules: {
|
||||
type: Object,
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
},
|
||||
// 校验触发器方式,默认 关闭
|
||||
validateTrigger: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
// label 位置,可选值 top/left
|
||||
labelPosition: {
|
||||
type: String,
|
||||
default: 'left'
|
||||
},
|
||||
// label 宽度,单位 px
|
||||
labelWidth: {
|
||||
type: [String, Number],
|
||||
default: 65
|
||||
},
|
||||
// label 居中方式,可选值 left/center/right
|
||||
labelAlign: {
|
||||
type: String,
|
||||
default: 'left'
|
||||
},
|
||||
errShowType: {
|
||||
type: String,
|
||||
default: 'undertext'
|
||||
},
|
||||
border: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
formData: {}
|
||||
};
|
||||
},
|
||||
watch: {
|
||||
rules(newVal) {
|
||||
this.init(newVal)
|
||||
},
|
||||
trigger(trigger) {
|
||||
this.formTrigger = trigger
|
||||
},
|
||||
value: {
|
||||
handler(newVal) {
|
||||
if (this.isChildEdit) {
|
||||
this.isChildEdit = false
|
||||
return
|
||||
}
|
||||
this.childrens.forEach((item) => {
|
||||
if (item.name) {
|
||||
const formDataValue = newVal.hasOwnProperty(item.name) ? newVal[item.name] : null
|
||||
this.formData[item.name] = this._getValue(item, formDataValue)
|
||||
}
|
||||
})
|
||||
},
|
||||
deep: true
|
||||
}
|
||||
},
|
||||
created() {
|
||||
let _this = this
|
||||
this.childrens = []
|
||||
this.inputChildrens = []
|
||||
this.checkboxChildrens = []
|
||||
this.formRules = []
|
||||
this.init(this.rules)
|
||||
},
|
||||
methods: {
|
||||
init(formRules) {
|
||||
if (Object.keys(formRules).length > 0) {
|
||||
this.formTrigger = this.trigger
|
||||
this.formRules = formRules
|
||||
if (!this.validator) {
|
||||
this.validator = new Validator(formRules)
|
||||
}
|
||||
}
|
||||
this.childrens.forEach((item) => {
|
||||
item.init()
|
||||
})
|
||||
},
|
||||
/**
|
||||
* 设置校验规则
|
||||
* @param {Object} formRules
|
||||
*/
|
||||
setRules(formRules) {
|
||||
this.init(formRules)
|
||||
},
|
||||
/**
|
||||
* 公开给用户使用
|
||||
* 设置自定义表单组件 value 值
|
||||
* @param {String} name 字段名称
|
||||
* @param {String} value 字段值
|
||||
*/
|
||||
setValue(name, value, callback) {
|
||||
let example = this.childrens.find(child => child.name === name)
|
||||
if (!example) return null
|
||||
this.isChildEdit = true
|
||||
value = this._getValue(example, value)
|
||||
this.formData[name] = value
|
||||
example.val = value
|
||||
this.$emit('input', Object.assign({}, this.value, this.formData))
|
||||
return example.triggerCheck(value, callback)
|
||||
},
|
||||
|
||||
/**
|
||||
* TODO 表单提交, 小程序暂不支持这种用法
|
||||
* @param {Object} event
|
||||
*/
|
||||
submitForm(event) {
|
||||
const value = event.detail.value
|
||||
return this.validateAll(value || this.formData, 'submit')
|
||||
},
|
||||
/**
|
||||
* 表单重置
|
||||
* @param {Object} event
|
||||
*/
|
||||
resetForm(event) {
|
||||
this.childrens.forEach(item => {
|
||||
item.errMsg = ''
|
||||
const inputComp = this.inputChildrens.find(child => child.rename === item.name)
|
||||
if (inputComp) {
|
||||
inputComp.errMsg = ''
|
||||
inputComp.$emit('input', inputComp.multiple?[]:'')
|
||||
}
|
||||
})
|
||||
|
||||
this.isChildEdit = true
|
||||
this.childrens.forEach((item) => {
|
||||
if (item.name) {
|
||||
this.formData[item.name] = this._getValue(item, '')
|
||||
}
|
||||
})
|
||||
|
||||
this.$emit('input', this.formData)
|
||||
this.$emit('reset', event)
|
||||
},
|
||||
|
||||
/**
|
||||
* 触发表单校验,通过 @validate 获取
|
||||
* @param {Object} validate
|
||||
*/
|
||||
validateCheck(validate) {
|
||||
if (validate === null) validate = null
|
||||
this.$emit('validate', validate)
|
||||
},
|
||||
/**
|
||||
* 校验所有或者部分表单
|
||||
*/
|
||||
async validateAll(invalidFields, type, callback) {
|
||||
|
||||
this.childrens.forEach(item => {
|
||||
item.errMsg = ''
|
||||
})
|
||||
|
||||
let promise;
|
||||
if (!callback && typeof callback !== 'function' && Promise) {
|
||||
promise = new Promise((resolve, reject) => {
|
||||
callback = function(valid, invalidFields) {
|
||||
!valid ? resolve(invalidFields) : reject(valid);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
let fieldsValue = {}
|
||||
let tempInvalidFields = Object.assign({}, invalidFields)
|
||||
|
||||
Object.keys(this.formRules).forEach(item => {
|
||||
const values = this.formRules[item]
|
||||
const rules = (values && values.rules) || []
|
||||
let isNoField = false
|
||||
for (let i = 0; i < rules.length; i++) {
|
||||
const rule = rules[i]
|
||||
if (rule.required) {
|
||||
isNoField = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 如果存在 required 才会将内容插入校验对象
|
||||
if (!isNoField && (!tempInvalidFields[item] && tempInvalidFields[item] !== false)) {
|
||||
delete tempInvalidFields[item]
|
||||
}
|
||||
|
||||
})
|
||||
// 循环字段是否存在于校验规则中
|
||||
for (let i in this.formRules) {
|
||||
for (let j in tempInvalidFields) {
|
||||
if (i === j) {
|
||||
fieldsValue[i] = tempInvalidFields[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
let result = []
|
||||
let example = null
|
||||
if (this.validator) {
|
||||
for (let i in fieldsValue) {
|
||||
const resultData = await this.validator.validateUpdate({
|
||||
[i]: fieldsValue[i]
|
||||
}, this.formData)
|
||||
if (resultData) {
|
||||
example = this.childrens.find(child => child.name === resultData.key)
|
||||
const inputComp = this.inputChildrens.find(child => child.rename === example.name)
|
||||
if (inputComp) {
|
||||
inputComp.errMsg = resultData.errorMessage
|
||||
}
|
||||
result.push(resultData)
|
||||
if (this.errShowType === 'undertext') {
|
||||
if (example) example.errMsg = resultData.errorMessage
|
||||
} else {
|
||||
if (this.errShowType === 'toast') {
|
||||
uni.showToast({
|
||||
title: resultData.errorMessage || '校验错误',
|
||||
icon: 'none'
|
||||
})
|
||||
break
|
||||
} else if (this.errShowType === 'modal') {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: resultData.errorMessage || '校验错误'
|
||||
})
|
||||
break
|
||||
} else {
|
||||
if (example) example.errMsg = resultData.errorMessage
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Array.isArray(result)) {
|
||||
if (result.length === 0) result = null
|
||||
}
|
||||
if (type === 'submit') {
|
||||
this.$emit('submit', {
|
||||
detail: {
|
||||
value: invalidFields,
|
||||
errors: result
|
||||
}
|
||||
})
|
||||
} else {
|
||||
this.$emit('validate', result)
|
||||
}
|
||||
callback && typeof callback === 'function' && callback(result, invalidFields)
|
||||
if (promise && callback) {
|
||||
return promise
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 外部调用方法
|
||||
* 手动提交校验表单
|
||||
* 对整个表单进行校验的方法,参数为一个回调函数。
|
||||
*/
|
||||
submit(callback) {
|
||||
// Object.assign(this.formData,formData)
|
||||
return this.validateAll(this.formData, 'submit', callback)
|
||||
},
|
||||
|
||||
/**
|
||||
* 外部调用方法
|
||||
* 校验表单
|
||||
* 对整个表单进行校验的方法,参数为一个回调函数。
|
||||
*/
|
||||
validate(callback) {
|
||||
return this.validateAll(this.formData, '', callback)
|
||||
},
|
||||
|
||||
/**
|
||||
* 部分表单校验
|
||||
* @param {Object} props
|
||||
* @param {Object} cb
|
||||
*/
|
||||
validateField(props, callback) {
|
||||
props = [].concat(props);
|
||||
let invalidFields = {}
|
||||
this.childrens.forEach(item => {
|
||||
// item.parentVal((val, name) => {
|
||||
if (props.indexOf(item.name) !== -1) {
|
||||
invalidFields = Object.assign({}, invalidFields, {
|
||||
[item.name]: this.formData[item.name]
|
||||
})
|
||||
}
|
||||
// })
|
||||
|
||||
})
|
||||
return this.validateAll(invalidFields, '', callback)
|
||||
},
|
||||
|
||||
/**
|
||||
* 对整个表单进行重置,将所有字段值重置为初始值并移除校验结果
|
||||
*/
|
||||
resetFields() {
|
||||
this.resetForm()
|
||||
},
|
||||
|
||||
/**
|
||||
* 移除表单项的校验结果。传入待移除的表单项的 prop 属性或者 prop 组成的数组,如不传则移除整个表单的校验结果
|
||||
*/
|
||||
clearValidate(props) {
|
||||
props = [].concat(props);
|
||||
this.childrens.forEach(item => {
|
||||
const inputComp = this.inputChildrens.find(child => child.rename === item.name)
|
||||
if (props.length === 0) {
|
||||
item.errMsg = ''
|
||||
if (inputComp) {
|
||||
inputComp.errMsg = ''
|
||||
}
|
||||
} else {
|
||||
if (props.indexOf(item.name) !== -1) {
|
||||
item.errMsg = ''
|
||||
if (inputComp) {
|
||||
inputComp.errMsg = ''
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
// 把 value 转换成指定的类型
|
||||
_getValue(item, value) {
|
||||
const rules = item.formRules.rules || []
|
||||
const isRuleNum = rules.find(val => val.format && this.type_filter(val.format))
|
||||
const isRuleBool = rules.find(val => val.format && val.format === 'boolean' || val.format === 'bool')
|
||||
// 输入值为 number
|
||||
if (isRuleNum) {
|
||||
value = value === '' || value === null ? null : Number(value)
|
||||
}
|
||||
// 简单判断真假值
|
||||
if (isRuleBool) {
|
||||
value = !value ? false : true
|
||||
}
|
||||
return value
|
||||
},
|
||||
// 过滤数字类型
|
||||
type_filter(format) {
|
||||
return format === 'int' || format === 'double' || format === 'number'
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.uni-forms {
|
||||
overflow: hidden;
|
||||
// padding: 10px 15px;
|
||||
// background-color: #fff;
|
||||
}
|
||||
|
||||
.uni-forms--top {
|
||||
padding: 10px 15px;
|
||||
// padding-top: 22px;
|
||||
}
|
||||
</style>
|
442
uni_modules/uni-ui/components/uni-forms/validate.js
Normal file
442
uni_modules/uni-ui/components/uni-forms/validate.js
Normal file
@ -0,0 +1,442 @@
|
||||
|
||||
var pattern = {
|
||||
email: /^\S+?@\S+?\.\S+?$/,
|
||||
url: new RegExp("^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$", 'i')
|
||||
};
|
||||
|
||||
const FORMAT_MAPPING = {
|
||||
"int": 'number',
|
||||
"bool": 'boolean',
|
||||
"double": 'number',
|
||||
"long": 'number',
|
||||
"password": 'string'
|
||||
}
|
||||
|
||||
function formatMessage(args, resources) {
|
||||
var defaultMessage = ['label']
|
||||
defaultMessage.forEach((item) => {
|
||||
if (args[item] === undefined) {
|
||||
args[item] = ''
|
||||
}
|
||||
})
|
||||
|
||||
let str = resources
|
||||
for (let key in args) {
|
||||
let reg = new RegExp('{' + key + '}')
|
||||
str = str.replace(reg, args[key])
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
function isEmptyValue(value, type) {
|
||||
if (value === undefined || value === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (typeof value === 'string' && !value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Array.isArray(value) && !value.length) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (type === 'object' && !Object.keys(value).length) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
const types = {
|
||||
integer(value) {
|
||||
return types.number(value) && parseInt(value, 10) === value;
|
||||
},
|
||||
string(value) {
|
||||
return typeof value === 'string';
|
||||
},
|
||||
number(value) {
|
||||
if (isNaN(value)) {
|
||||
return false;
|
||||
}
|
||||
return typeof value === 'number';
|
||||
},
|
||||
"boolean": function (value) {
|
||||
return typeof value === 'boolean';
|
||||
},
|
||||
"float": function (value) {
|
||||
return types.number(value) && !types.integer(value);
|
||||
},
|
||||
array(value) {
|
||||
return Array.isArray(value);
|
||||
},
|
||||
object(value) {
|
||||
return typeof value === 'object' && !types.array(value);
|
||||
},
|
||||
date(value) {
|
||||
var v
|
||||
if (value instanceof Date) {
|
||||
v = value;
|
||||
} else {
|
||||
v = new Date(value);
|
||||
}
|
||||
return typeof v.getTime === 'function' && typeof v.getMonth === 'function' && typeof v.getYear === 'function' && !isNaN(v.getTime());
|
||||
},
|
||||
timestamp(value) {
|
||||
if (!this.integer(value) || Math.abs(value).toString().length > 16) {
|
||||
return false
|
||||
}
|
||||
|
||||
return this.date(value);
|
||||
},
|
||||
email(value) {
|
||||
return typeof value === 'string' && !!value.match(pattern.email) && value.length < 255;
|
||||
},
|
||||
url(value) {
|
||||
return typeof value === 'string' && !!value.match(pattern.url);
|
||||
},
|
||||
pattern(reg, value) {
|
||||
try {
|
||||
return new RegExp(reg).test(value);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
method(value) {
|
||||
return typeof value === 'function';
|
||||
}
|
||||
}
|
||||
|
||||
class RuleValidator {
|
||||
|
||||
constructor(message) {
|
||||
this._message = message
|
||||
}
|
||||
|
||||
async validateRule(key, value, data, allData) {
|
||||
var result = null
|
||||
|
||||
let rules = key.rules
|
||||
|
||||
let hasRequired = rules.findIndex((item) => {
|
||||
return item.required
|
||||
})
|
||||
if (hasRequired < 0) {
|
||||
if (value === null || value === undefined) {
|
||||
return result
|
||||
}
|
||||
if (typeof value === 'string' && !value.length) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
var message = this._message
|
||||
|
||||
if (rules === undefined) {
|
||||
return message['default']
|
||||
}
|
||||
|
||||
for (var i = 0; i < rules.length; i++) {
|
||||
let rule = rules[i]
|
||||
let vt = this._getValidateType(rule)
|
||||
|
||||
if (key.label !== undefined) {
|
||||
Object.assign(rule, {
|
||||
label: key.label
|
||||
})
|
||||
}
|
||||
|
||||
if (RuleValidatorHelper[vt]) {
|
||||
result = RuleValidatorHelper[vt](rule, value, message)
|
||||
if (result != null) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (rule.validateExpr) {
|
||||
let now = Date.now()
|
||||
let resultExpr = rule.validateExpr(value, allData, now)
|
||||
if (resultExpr === false) {
|
||||
result = this._getMessage(rule, rule.errorMessage || this._message['default'])
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (rule.validateFunction) {
|
||||
result = await this.validateFunction(rule, value, data, allData, vt)
|
||||
if (result !== null) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
async validateFunction(rule, value, data, allData, vt) {
|
||||
let result = null
|
||||
try {
|
||||
let callbackMessage = null
|
||||
const res = await rule.validateFunction(rule, value, allData || data, (message) => {
|
||||
callbackMessage = message
|
||||
})
|
||||
if (callbackMessage || (typeof res === 'string' && res) || res === false) {
|
||||
result = this._getMessage(rule, callbackMessage || res, vt)
|
||||
}
|
||||
} catch (e) {
|
||||
result = this._getMessage(rule, e.message, vt)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
_getMessage(rule, message, vt) {
|
||||
return formatMessage(rule, message || rule.errorMessage || this._message[vt] || message['default'])
|
||||
}
|
||||
|
||||
_getValidateType(rule) {
|
||||
// TODO
|
||||
var result = ''
|
||||
if (rule.required) {
|
||||
result = 'required'
|
||||
} else if (rule.format) {
|
||||
result = 'format'
|
||||
} else if (rule.range) {
|
||||
result = 'range'
|
||||
} else if (rule.maximum || rule.minimum) {
|
||||
result = 'rangeNumber'
|
||||
} else if (rule.maxLength || rule.minLength) {
|
||||
result = 'rangeLength'
|
||||
} else if (rule.pattern) {
|
||||
result = 'pattern'
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
const RuleValidatorHelper = {
|
||||
required(rule, value, message) {
|
||||
if (rule.required && isEmptyValue(value, rule.format || typeof value)) {
|
||||
return formatMessage(rule, rule.errorMessage || message.required);
|
||||
}
|
||||
|
||||
return null
|
||||
},
|
||||
|
||||
range(rule, value, message) {
|
||||
const { range, errorMessage } = rule;
|
||||
|
||||
let list = new Array(range.length);
|
||||
for (let i = 0; i < range.length; i++) {
|
||||
const item = range[i];
|
||||
if (types.object(item) && item.value !== undefined) {
|
||||
list[i] = item.value;
|
||||
} else {
|
||||
list[i] = item;
|
||||
}
|
||||
}
|
||||
|
||||
let result = false
|
||||
if (Array.isArray(value)) {
|
||||
result = (new Set(value.concat(list)).size === list.length);
|
||||
} else {
|
||||
if (list.indexOf(value) > -1) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
return formatMessage(rule, errorMessage || message['enum']);
|
||||
}
|
||||
|
||||
return null
|
||||
},
|
||||
|
||||
rangeNumber(rule, value, message) {
|
||||
if (!types.number(value)) {
|
||||
return formatMessage(rule, rule.errorMessage || message.pattern.mismatch);
|
||||
}
|
||||
|
||||
let { minimum, maximum, exclusiveMinimum, exclusiveMaximum } = rule;
|
||||
let min = exclusiveMinimum ? value <= minimum : value < minimum;
|
||||
let max = exclusiveMaximum ? value >= maximum : value > maximum;
|
||||
|
||||
if (minimum !== undefined && min) {
|
||||
return formatMessage(rule, rule.errorMessage || message['number'].min)
|
||||
} else if (maximum !== undefined && max) {
|
||||
return formatMessage(rule, rule.errorMessage || message['number'].max)
|
||||
} else if (minimum !== undefined && maximum !== undefined && (min || max)) {
|
||||
return formatMessage(rule, rule.errorMessage || message['number'].range)
|
||||
}
|
||||
|
||||
return null
|
||||
},
|
||||
|
||||
rangeLength(rule, value, message) {
|
||||
if (!types.string(value) && !types.array(value)) {
|
||||
return formatMessage(rule, rule.errorMessage || message.pattern.mismatch);
|
||||
}
|
||||
|
||||
let min = rule.minLength;
|
||||
let max = rule.maxLength;
|
||||
let val = value.length;
|
||||
|
||||
if (min !== undefined && val < min) {
|
||||
return formatMessage(rule, rule.errorMessage || message['length'].min)
|
||||
} else if (max !== undefined && val > max) {
|
||||
return formatMessage(rule, rule.errorMessage || message['length'].max)
|
||||
} else if (min !== undefined && max !== undefined && (val < min || val > max)) {
|
||||
return formatMessage(rule, rule.errorMessage || message['length'].range)
|
||||
}
|
||||
|
||||
return null
|
||||
},
|
||||
|
||||
pattern(rule, value, message) {
|
||||
if (!types['pattern'](rule.pattern, value)) {
|
||||
return formatMessage(rule, rule.errorMessage || message.pattern.mismatch);
|
||||
}
|
||||
|
||||
return null
|
||||
},
|
||||
|
||||
format(rule, value, message) {
|
||||
var customTypes = Object.keys(types);
|
||||
var format = FORMAT_MAPPING[rule.format] ? FORMAT_MAPPING[rule.format] : rule.format;
|
||||
|
||||
if (customTypes.indexOf(format) > -1) {
|
||||
if (!types[format](value)) {
|
||||
return formatMessage(rule, rule.errorMessage || message.types[format]);
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
class SchemaValidator extends RuleValidator {
|
||||
|
||||
constructor(schema, options) {
|
||||
super(SchemaValidator.message);
|
||||
|
||||
this._schema = schema
|
||||
this._options = options || null
|
||||
}
|
||||
|
||||
updateSchema(schema) {
|
||||
this._schema = schema
|
||||
}
|
||||
|
||||
async validate(data, allData) {
|
||||
let result = this._checkFieldInSchema(data)
|
||||
if (!result) {
|
||||
result = await this.invokeValidate(data, false, allData)
|
||||
}
|
||||
return result.length ? result[0] : null
|
||||
}
|
||||
|
||||
async validateAll(data, allData) {
|
||||
let result = this._checkFieldInSchema(data)
|
||||
if (!result) {
|
||||
result = await this.invokeValidate(data, true, allData)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
async validateUpdate(data, allData) {
|
||||
let result = this._checkFieldInSchema(data)
|
||||
if (!result) {
|
||||
result = await this.invokeValidateUpdate(data, false, allData)
|
||||
}
|
||||
return result.length ? result[0] : null
|
||||
}
|
||||
|
||||
async invokeValidate(data, all, allData) {
|
||||
let result = []
|
||||
let schema = this._schema
|
||||
for (let key in schema) {
|
||||
let value = schema[key]
|
||||
let errorMessage = await this.validateRule(value, data[key], data, allData)
|
||||
if (errorMessage != null) {
|
||||
result.push({
|
||||
key,
|
||||
errorMessage
|
||||
})
|
||||
if (!all) break
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
async invokeValidateUpdate(data, all, allData) {
|
||||
let result = []
|
||||
for (let key in data) {
|
||||
let errorMessage = await this.validateRule(this._schema[key], data[key], data, allData)
|
||||
if (errorMessage != null) {
|
||||
result.push({
|
||||
key,
|
||||
errorMessage
|
||||
})
|
||||
if (!all) break
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
_checkFieldInSchema(data) {
|
||||
var keys = Object.keys(data)
|
||||
var keys2 = Object.keys(this._schema)
|
||||
if (new Set(keys.concat(keys2)).size === keys2.length) {
|
||||
return ''
|
||||
}
|
||||
return [{
|
||||
key: 'invalid',
|
||||
errorMessage: SchemaValidator.message['defaultInvalid']
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
function Message() {
|
||||
return {
|
||||
default: '验证错误',
|
||||
defaultInvalid: '字段超出范围',
|
||||
required: '{label}必填',
|
||||
'enum': '{label}超出范围',
|
||||
whitespace: '{label}不能为空',
|
||||
date: {
|
||||
format: '{label}日期{value}格式无效',
|
||||
parse: '{label}日期无法解析,{value}无效',
|
||||
invalid: '{label}日期{value}无效'
|
||||
},
|
||||
types: {
|
||||
string: '{label}类型无效',
|
||||
array: '{label}类型无效',
|
||||
object: '{label}类型无效',
|
||||
number: '{label}类型无效',
|
||||
date: '{label}类型无效',
|
||||
boolean: '{label}类型无效',
|
||||
integer: '{label}类型无效',
|
||||
float: '{label}类型无效',
|
||||
regexp: '{label}无效',
|
||||
email: '{label}类型无效',
|
||||
url: '{label}类型无效'
|
||||
},
|
||||
length: {
|
||||
min: '{label}长度不能少于{minLength}',
|
||||
max: '{label}长度不能超过{maxLength}',
|
||||
range: '{label}必须介于{minLength}和{maxLength}之间'
|
||||
},
|
||||
number: {
|
||||
min: '{label}不能小于{minimum}',
|
||||
max: '{label}不能大于{maximum}',
|
||||
range: '{label}必须介于{minimum}and{maximum}之间'
|
||||
},
|
||||
pattern: {
|
||||
mismatch: '{label}格式不匹配'
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
SchemaValidator.message = new Message();
|
||||
|
||||
export default SchemaValidator
|
Reference in New Issue
Block a user