Skip to content

Commit

Permalink
refactor: 重构useValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
kailong321200875 committed Aug 16, 2023
1 parent 71b0e4f commit b8849da
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 71 deletions.
9 changes: 9 additions & 0 deletions mock/role/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,14 @@ const adminList = [
meta: {
title: 'useTagsView'
}
},
{
path: 'useValidator',
component: 'views/hooks/useValidator',
name: 'UseValidator',
meta: {
title: 'useValidator'
}
}
// {
// path: 'useCrudSchemas',
Expand Down Expand Up @@ -598,6 +606,7 @@ const testList: string[] = [
'/hooks',
'/hooks/useWatermark',
'/hooks/useTagsView',
'/hooks/useValidator',
// '/hooks/useCrudSchemas',
'/level',
'/level/menu1',
Expand Down
49 changes: 49 additions & 0 deletions src/hooks/web/useGuide.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Config, driver } from 'driver.js'
import 'driver.js/dist/driver.css'
import { useDesign } from '@/hooks/web/useDesign'
import { useI18n } from '@/hooks/web/useI18n'

const { t } = useI18n()

const { variables } = useDesign()

export const useGuide = (options?: Config) => {
const driverObj = driver(
options || {
showProgress: true,
nextBtnText: t('common.nextLabel'),
prevBtnText: t('common.prevLabel'),
doneBtnText: t('common.doneLabel'),
steps: [
{
element: `#${variables.namespace}-menu`,
popover: {
title: t('common.menu'),
description: t('common.menuDes'),
side: 'right'
}
},
{
element: `#${variables.namespace}-tool-header`,
popover: {
title: t('common.tool'),
description: t('common.toolDes'),
side: 'left'
}
},
{
element: `#${variables.namespace}-tags-view`,
popover: {
title: t('common.tagsView'),
description: t('common.tagsViewDes'),
side: 'bottom'
}
}
]
}
)

return {
...driverObj
}
}
58 changes: 27 additions & 31 deletions src/hooks/web/useValidator.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,60 @@
import { useI18n } from '@/hooks/web/useI18n'
import { FormItemRule } from 'element-plus'

const { t } = useI18n()

type Callback = (error?: string | Error | undefined) => void

interface LengthRange {
min: number
max: number
message: string
message?: string
}

export const useValidator = () => {
const required = (message?: string) => {
const required = (message?: string): FormItemRule => {
return {
required: true,
message: message || t('common.required')
}
}

const lengthRange = (val: any, callback: Callback, options: LengthRange) => {
const lengthRange = (options: LengthRange): FormItemRule => {
const { min, max, message } = options
if (val.length < min || val.length > max) {
callback(new Error(message))
} else {
callback()
}
}

const notSpace = (val: any, callback: Callback, message: string) => {
// 用户名不能有空格
if (val.indexOf(' ') !== -1) {
callback(new Error(message))
} else {
callback()
return {
min,
max,
message: message || t('common.lengthRange', { min, max })
}
}

const notSpecialCharacters = (val: any, callback: Callback, message: string) => {
// 密码不能是特殊字符
if (/[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/gi.test(val)) {
callback(new Error(message))
} else {
callback()
const notSpace = (message?: string): FormItemRule => {
return {
validator: (_, val, callback) => {
if (val.indexOf(' ') !== -1) {
callback(new Error(message || t('common.notSpace')))
} else {
callback()
}
}
}
}

// 两个字符串是否想等
const isEqual = (val1: string, val2: string, callback: Callback, message: string) => {
if (val1 === val2) {
callback()
} else {
callback(new Error(message))
const notSpecialCharacters = (message?: string): FormItemRule => {
return {
validator: (_, val, callback) => {
if (/[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/gi.test(val)) {
callback(new Error(message || t('common.notSpecialCharacters')))
} else {
callback()
}
}
}
}

return {
required,
lengthRange,
notSpace,
notSpecialCharacters,
isEqual
notSpecialCharacters
}
}
6 changes: 5 additions & 1 deletion src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ export default {
refresh: 'Refresh',
fullscreen: 'Fullscreen',
size: 'Size',
columnSetting: 'Column setting'
columnSetting: 'Column setting',
lengthRange: 'The length should be between {min} and {max}',
notSpace: 'Spaces are not allowed',
notSpecialCharacters: 'Special characters are not allowed',
isEqual: 'The two are not equal'
},
lock: {
lockScreen: 'Lock screen',
Expand Down
6 changes: 5 additions & 1 deletion src/locales/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ export default {
refresh: '刷新',
fullscreen: '全屏',
size: '尺寸',
columnSetting: '列设置'
columnSetting: '列设置',
lengthRange: '长度在 {min} 到 {max} 个字符',
notSpace: '不能包含空格',
notSpecialCharacters: '不能包含特殊字符',
isEqual: '两次输入不一致'
},
lock: {
lockScreen: '锁定屏幕',
Expand Down
8 changes: 8 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,14 @@ export const asyncRouterMap: AppRouteRecordRaw[] = [
meta: {
title: 'useTagsView'
}
},
{
path: 'useValidator',
component: () => import('@/views/hooks/useValidator.vue'),
name: 'UseValidator',
meta: {
title: 'useValidator'
}
}
// {
// path: 'useCrudSchemas',
Expand Down
41 changes: 3 additions & 38 deletions src/views/Guide/Guide.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,14 @@
import { ContentWrap } from '@/components/ContentWrap'
import { useI18n } from '@/hooks/web/useI18n'
import { ElButton } from 'element-plus'
import { driver } from 'driver.js'
import 'driver.js/dist/driver.css'
import { useDesign } from '@/hooks/web/useDesign'
import { useGuide } from '@/hooks/web/useGuide'
const { t } = useI18n()
const { variables } = useDesign()
const driverObj = driver({
showProgress: true,
nextBtnText: t('common.nextLabel'),
prevBtnText: t('common.prevLabel'),
doneBtnText: t('common.doneLabel'),
steps: [
{
element: `#${variables.namespace}-menu`,
popover: {
title: t('common.menu'),
description: t('common.menuDes'),
side: 'right'
}
},
{
element: `#${variables.namespace}-tool-header`,
popover: {
title: t('common.tool'),
description: t('common.toolDes'),
side: 'left'
}
},
{
element: `#${variables.namespace}-tags-view`,
popover: {
title: t('common.tagsView'),
description: t('common.tagsViewDes'),
side: 'bottom'
}
}
]
})
const { drive } = useGuide()
const guideStart = () => {
driverObj.drive()
drive()
}
</script>

Expand Down
80 changes: 80 additions & 0 deletions src/views/hooks/useValidator.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { Form, FormSchema } from '@/components/Form'
import { useValidator } from '@/hooks/web/useValidator'
import { useForm } from '@/hooks/web/useForm'
import { reactive } from 'vue'
import { FormItemRule } from 'element-plus'
const { formRegister, formMethods } = useForm()
const { getFormData } = formMethods
const { required, lengthRange, notSpace, notSpecialCharacters } = useValidator()
const formSchema = reactive<FormSchema[]>([
{
field: 'field1',
label: '必填',
component: 'Input'
},
{
field: 'field2',
label: '长度范围',
component: 'Input'
},
{
field: 'field3',
label: '不能有空格',
component: 'Input'
},
{
field: 'field4',
label: '不能有特殊字符',
component: 'Input'
},
{
field: 'field5',
label: '是否相等-值1',
component: 'Input'
},
{
field: 'field6',
label: '是否相等-值2',
component: 'Input'
}
])
const rules = reactive<{
[key: string]: FormItemRule[]
}>({
field1: [required()],
field2: [
lengthRange({
min: 2,
max: 5
})
],
field3: [notSpace()],
field4: [notSpecialCharacters()],
field5: [
{
asyncValidator: async (_, val, callback) => {
const formData = await getFormData()
const { field6 } = formData
if (val !== field6) {
callback(new Error('两个值不相等'))
} else {
callback()
}
}
}
]
})
</script>

<template>
<ContentWrap title="useValidator">
<Form :schema="formSchema" :rules="rules" @register="formRegister" />
</ContentWrap>
</template>

0 comments on commit b8849da

Please sign in to comment.