perf: useValidator hooks

This commit is contained in:
xingyu 2023-10-31 10:11:43 +08:00
parent f8fdebffe6
commit bdcef303ff

View File

@ -1,54 +1,53 @@
const { t } = useI18n() import { useI18n } from '@/hooks/web/useI18n'
import { FormItemRule } from 'element-plus'
type Callback = (error?: string | Error | undefined) => void const { t } = useI18n()
interface LengthRange { interface LengthRange {
min: number min: number
max: number max: number
message: string message?: string
} }
export const useValidator = () => { export const useValidator = () => {
const required = (message?: string) => { const required = (message?: string): FormItemRule => {
return { return {
required: true, required: true,
message: message || t('common.required') message: message || t('common.required')
} }
} }
const lengthRange = (val: any, callback: Callback, options: LengthRange) => { const lengthRange = (options: LengthRange): FormItemRule => {
const { min, max, message } = options const { min, max, message } = options
if (val.length < min || val.length > max) {
callback(new Error(message)) return {
min,
max,
message: message || t('common.lengthRange', { min, max })
}
}
const notSpace = (message?: string): FormItemRule => {
return {
validator: (_, val, callback) => {
if (val?.indexOf(' ') !== -1) {
callback(new Error(message || t('common.notSpace')))
} else { } else {
callback() callback()
} }
} }
const notSpace = (val: any, callback: Callback, message: string) => {
// 用户名不能有空格
if (val.indexOf(' ') !== -1) {
callback(new Error(message))
} else {
callback()
} }
} }
const notSpecialCharacters = (val: any, callback: Callback, message: string) => { const notSpecialCharacters = (message?: string): FormItemRule => {
// 密码不能是特殊字符 return {
validator: (_, val, callback) => {
if (/[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/gi.test(val)) { if (/[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/gi.test(val)) {
callback(new Error(message)) callback(new Error(message || t('common.notSpecialCharacters')))
} else { } else {
callback() callback()
} }
} }
// 两个字符串是否想等
const isEqual = (val1: string, val2: string, callback: Callback, message: string) => {
if (val1 === val2) {
callback()
} else {
callback(new Error(message))
} }
} }
@ -56,7 +55,6 @@ export const useValidator = () => {
required, required,
lengthRange, lengthRange,
notSpace, notSpace,
notSpecialCharacters, notSpecialCharacters
isEqual
} }
} }