Skip to content

refactor(form): 提取校验函数到use-validate #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions packages/devui-vue/devui/form/src/use-validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import AsyncValidator from 'async-validator';

export default function useValidate() {

// 校验函数
const validate = (descriptor, validateObject) => {
const validator = new AsyncValidator(descriptor);
return validator.validate(validateObject);
}

// 创建内置校验器
const createDevUIBuiltinValidator = (rule) => {
let res = {...rule};
if(res.min !== undefined) {
res = {
...res,
message: res.message ?? `最小值为${res.min}`,
asyncValidator: (r, val) => {
return new Promise((resolve, reject) => {
if(val < res.min) {
reject('最小值为' + res.min);
}else {
resolve('校验通过');
}
})
}
}
}

if(res.max !== undefined) {
res = {
...res,
message: res.message ?? `最大值为${res.max}`,
asyncValidator: (r, val) => {
return new Promise((resolve, reject) => {
if(val > res.max) {
reject('最大值为' + res.max);
}else {
resolve('校验通过');
}
})
}
}
}

if(res.maxlength !== undefined) {
res = {
...res,
max: res.maxlength,
message: res.message ?? `最大长度为${res.maxlength}`
}
delete res.maxlength;
delete res.asyncValidator;
}

if(res.minlength !== undefined) {
res = {
...res,
min: res.minlength,
message: res.message ?? `最小长度为${res.minlength}`
}
delete res.minlength;
delete res.asyncValidator;
}


if(res.requiredTrue !== undefined) {
res = {
...res,
message: res.message ?? `必须为true值`,
asyncValidator: (r, val) => {
return new Promise((resolve, reject) => {
if(!val) {
reject('必须为true值');
}else {
resolve('校验通过');
}
})
}
}
}
if(res.email !== undefined){
res = {
...res,
type: 'email',
message: res.message ?? '邮箱格式不正确'
}
delete res.asyncValidator;
}
if(res.pattern !== undefined){
res = {
...res,
type: 'pattern',
message: res.message ?? '正则不匹配'
}
delete res.asyncValidator;
}
if(res.whitespace === true){
res = {
...res,
type: 'string',
message: res.message ?? '不能全为空格',
asyncValidator: (r, val) => {
return new Promise((resolve, reject) => {
if(val.trim() === '') {
reject('不能全为空格');
}else {
resolve('校验通过');
}
})
}
}
}

return res;
}

return {validate, createDevUIBuiltinValidator};
}