Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
bdcef303
authored
Oct 31, 2023
by
xingyu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: useValidator hooks
parent
f8fdebff
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
25 deletions
+23
-25
src/hooks/web/useValidator.ts
+23
-25
No files found.
src/hooks/web/useValidator.ts
View file @
bdcef303
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
{
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
()
return
{
min
,
max
,
message
:
message
||
t
(
'common.lengthRange'
,
{
min
,
max
})
}
}
const
notSpace
=
(
val
:
any
,
callback
:
Callback
,
message
:
string
)
=>
{
// 用户名不能有空格
if
(
val
.
indexOf
(
' '
)
!==
-
1
)
{
callback
(
new
Error
(
message
))
const
notSpace
=
(
message
?:
string
):
FormItemRule
=>
{
return
{
validator
:
(
_
,
val
,
callback
)
=>
{
if
(
val
?.
indexOf
(
' '
)
!==
-
1
)
{
callback
(
new
Error
(
message
||
t
(
'common.notSpace'
)))
}
else
{
callback
()
}
}
}
}
const
notSpecialCharacters
=
(
val
:
any
,
callback
:
Callback
,
message
:
string
)
=>
{
// 密码不能是特殊字符
const
notSpecialCharacters
=
(
message
?:
string
):
FormItemRule
=>
{
return
{
validator
:
(
_
,
val
,
callback
)
=>
{
if
(
/
[
`~!@#$%^&*()_+<>?:"{},.
\/
;'[
\]]
/gi
.
test
(
val
))
{
callback
(
new
Error
(
message
))
callback
(
new
Error
(
message
||
t
(
'common.notSpecialCharacters'
)
))
}
else
{
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
,
lengthRange
,
notSpace
,
notSpecialCharacters
,
isEqual
notSpecialCharacters
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment