Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello_uni-id-pages
提交
2faaec33
H
hello_uni-id-pages
项目概览
DCloud
/
hello_uni-id-pages
通知
1045
Star
30
Fork
43
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
2
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hello_uni-id-pages
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
2
合并请求
2
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2faaec33
编写于
7月 12, 2022
作者:
雪洛
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: add validator mixin
上级
3cff96e3
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
379 addition
and
65 deletion
+379
-65
uni_modules/uni-id-pages/.npmignore
uni_modules/uni-id-pages/.npmignore
+2
-0
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/validator.js
...ges/uniCloud/cloudfunctions/uni-id-co/common/validator.js
+212
-59
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/validator.test.js
...niCloud/cloudfunctions/uni-id-co/common/validator.test.js
+122
-0
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js
...i-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js
+42
-1
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/middleware/validate.js
.../uniCloud/cloudfunctions/uni-id-co/middleware/validate.js
+1
-5
未找到文件。
uni_modules/uni-id-pages/.npmignore
0 → 100644
浏览文件 @
2faaec33
*.test.js
**/node_modules/
\ No newline at end of file
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/validator.js
浏览文件 @
2faaec33
...
@@ -6,9 +6,9 @@ const {
...
@@ -6,9 +6,9 @@ const {
ERROR
ERROR
}
=
require
(
'
./error
'
)
}
=
require
(
'
./error
'
)
const
v
alidator
=
Object
.
create
(
null
)
const
baseV
alidator
=
Object
.
create
(
null
)
v
alidator
.
username
=
function
(
username
)
{
baseV
alidator
.
username
=
function
(
username
)
{
const
errCode
=
ERROR
.
INVALID_USERNAME
const
errCode
=
ERROR
.
INVALID_USERNAME
if
(
!
isValidString
(
username
))
{
if
(
!
isValidString
(
username
))
{
return
{
return
{
...
@@ -29,7 +29,7 @@ validator.username = function (username) {
...
@@ -29,7 +29,7 @@ validator.username = function (username) {
}
}
}
}
v
alidator
.
password
=
function
(
password
)
{
baseV
alidator
.
password
=
function
(
password
)
{
const
errCode
=
ERROR
.
INVALID_PASSWORD
const
errCode
=
ERROR
.
INVALID_PASSWORD
if
(
!
isValidString
(
password
))
{
if
(
!
isValidString
(
password
))
{
return
{
return
{
...
@@ -44,7 +44,7 @@ validator.password = function (password) {
...
@@ -44,7 +44,7 @@ validator.password = function (password) {
}
}
}
}
v
alidator
.
mobile
=
function
(
mobile
)
{
baseV
alidator
.
mobile
=
function
(
mobile
)
{
const
errCode
=
ERROR
.
INVALID_MOBILE
const
errCode
=
ERROR
.
INVALID_MOBILE
if
(
!
isValidString
(
mobile
))
{
if
(
!
isValidString
(
mobile
))
{
return
{
return
{
...
@@ -58,7 +58,7 @@ validator.mobile = function (mobile) {
...
@@ -58,7 +58,7 @@ validator.mobile = function (mobile) {
}
}
}
}
v
alidator
.
email
=
function
(
email
)
{
baseV
alidator
.
email
=
function
(
email
)
{
const
errCode
=
ERROR
.
INVALID_EMAIL
const
errCode
=
ERROR
.
INVALID_EMAIL
if
(
!
isValidString
(
email
))
{
if
(
!
isValidString
(
email
))
{
return
{
return
{
...
@@ -72,7 +72,7 @@ validator.email = function (email) {
...
@@ -72,7 +72,7 @@ validator.email = function (email) {
}
}
}
}
v
alidator
.
nickname
=
function
(
nickname
)
{
baseV
alidator
.
nickname
=
function
(
nickname
)
{
const
errCode
=
ERROR
.
INVALID_NICKNAME
const
errCode
=
ERROR
.
INVALID_NICKNAME
if
(
nickname
.
indexOf
(
'
@
'
)
!==
-
1
)
{
if
(
nickname
.
indexOf
(
'
@
'
)
!==
-
1
)
{
// 昵称不允许含@
// 昵称不允许含@
...
@@ -94,41 +94,194 @@ validator.nickname = function (nickname) {
...
@@ -94,41 +94,194 @@ validator.nickname = function (nickname) {
}
}
}
}
validate
[
'
array<string>
'
]
=
function
(
arr
)
{
const
baseType
=
[
'
string
'
,
'
boolean
'
,
'
number
'
,
'
null
'
]
// undefined不会由客户端提交上来
if
(
getType
(
arr
)
!==
'
array
'
)
{
baseType
.
forEach
((
type
)
=>
{
baseValidator
[
type
]
=
function
(
val
)
{
if
(
getType
(
val
)
===
type
)
{
return
}
return
{
return
{
errCode
:
ERROR
.
INVALID_PARAM
errCode
:
ERROR
.
INVALID_PARAM
}
}
}
}
if
(
arr
.
some
(
item
=>
!
isValidString
(
item
)))
{
})
return
{
errCode
:
ERROR
.
INVALID_PARAM
function
tokenize
(
name
)
{
let
i
=
0
const
result
=
[]
let
token
=
''
while
(
i
<
name
.
length
)
{
const
char
=
name
[
i
]
switch
(
char
)
{
case
'
|
'
:
case
'
<
'
:
case
'
>
'
:
token
&&
result
.
push
(
token
)
result
.
push
(
char
)
token
=
''
break
default
:
token
+=
char
break
}
i
++
if
(
i
===
name
.
length
&&
token
)
{
result
.
push
(
token
)
}
}
return
result
}
/**
* 处理validator名
* @param {string} name
*/
function
parseValidatorName
(
name
)
{
const
tokenList
=
tokenize
(
name
)
let
i
=
0
let
currentToken
=
tokenList
[
i
]
const
result
=
{
type
:
'
root
'
,
children
:
[],
parent
:
null
}
let
lastRealm
=
result
while
(
currentToken
)
{
switch
(
currentToken
)
{
case
'
array
'
:
{
const
currentRealm
=
{
type
:
'
array
'
,
children
:
[],
parent
:
lastRealm
}
lastRealm
.
children
.
push
(
currentRealm
)
lastRealm
=
currentRealm
break
}
case
'
<
'
:
if
(
lastRealm
.
type
!==
'
array
'
)
{
throw
new
Error
(
'
Invalid validator token "<"
'
)
}
break
case
'
>
'
:
if
(
lastRealm
.
type
!==
'
array
'
)
{
throw
new
Error
(
'
Invalid validator token ">"
'
)
}
lastRealm
=
lastRealm
.
parent
break
case
'
|
'
:
if
(
lastRealm
.
type
!==
'
array
'
&&
lastRealm
.
type
!==
'
root
'
)
{
throw
new
Error
(
'
Invalid validator token "|"
'
)
}
break
default
:
lastRealm
.
children
.
push
({
type
:
currentToken
})
break
}
i
++
currentToken
=
tokenList
[
i
]
}
return
result
}
function
getRuleCategory
(
rule
)
{
switch
(
rule
.
type
)
{
case
'
array
'
:
return
'
array
'
case
'
root
'
:
return
'
root
'
default
:
return
'
base
'
}
}
function
isMatchUnionType
(
val
,
rule
)
{
if
(
!
rule
.
children
||
rule
.
children
.
length
===
0
)
{
return
true
}
const
children
=
rule
.
children
for
(
let
i
=
0
;
i
<
children
.
length
;
i
++
)
{
const
child
=
children
[
i
]
const
category
=
getRuleCategory
(
child
)
let
pass
=
false
switch
(
category
)
{
case
'
base
'
:
pass
=
isMatchBaseType
(
val
,
child
)
break
case
'
array
'
:
pass
=
isMatchArrayType
(
val
,
child
)
break
default
:
break
}
if
(
pass
)
{
return
true
}
}
return
false
}
function
isMatchBaseType
(
val
,
rule
)
{
if
(
typeof
baseValidator
[
rule
.
type
]
!==
'
function
'
)
{
throw
new
Error
(
`invalid schema type:
${
rule
.
type
}
`
)
}
}
const
validateRes
=
baseValidator
[
rule
.
type
](
val
)
if
(
validateRes
&&
validateRes
.
errCode
)
{
return
false
}
}
return
true
}
}
const
baseType
=
[
'
string
'
,
'
boolean
'
,
'
number
'
]
function
isMatchArrayType
(
arr
,
rule
)
{
if
(
getType
(
arr
)
!==
'
array
'
)
{
return
false
}
if
(
rule
.
children
&&
rule
.
children
.
length
&&
arr
.
some
(
item
=>
!
isMatchUnionType
(
item
,
rule
)))
{
return
false
}
return
true
}
baseType
.
forEach
((
type
)
=>
{
class
Validator
{
validator
[
type
]
=
function
(
val
)
{
constructor
()
{
if
(
getType
(
val
)
!==
type
)
{
this
.
baseValidator
=
baseValidator
return
{
this
.
customValidator
=
Object
.
create
(
null
)
errCode
:
ERROR
.
INVALID_PARAM
}
}
mixin
(
type
,
handler
)
{
this
.
customValidator
[
type
]
=
handler
}
}
getRealBaseValidator
(
type
)
{
return
this
.
customValidator
[
type
]
||
this
.
baseValidator
[
type
]
}
}
})
validator
[
'
number|string
'
]
=
function
(
val
)
{
get
validator
()
{
const
type
=
getType
(
val
)
return
new
Proxy
({},
{
if
(
type
!==
'
number
'
&&
type
!==
'
string
'
)
{
get
:
(
_
,
prop
)
=>
{
if
(
typeof
prop
!==
'
string
'
)
{
return
}
const
realBaseValidator
=
this
.
getRealBaseValidator
(
prop
)
if
(
realBaseValidator
)
{
return
realBaseValidator
}
const
rule
=
parseValidatorName
(
prop
)
return
function
(
val
)
{
if
(
!
isMatchUnionType
(
val
,
rule
))
{
return
{
return
{
errCode
:
ERROR
.
INVALID_PARAM
errCode
:
ERROR
.
INVALID_PARAM
}
}
}
}
}
}
}
})
}
function
validate
(
value
=
{},
schema
=
{})
{
validate
(
value
=
{},
schema
=
{})
{
for
(
const
schemaKey
in
schema
)
{
for
(
const
schemaKey
in
schema
)
{
let
schemaValue
=
schema
[
schemaKey
]
let
schemaValue
=
schema
[
schemaKey
]
if
(
getType
(
schemaValue
)
===
'
string
'
)
{
if
(
getType
(
schemaValue
)
===
'
string
'
)
{
...
@@ -155,7 +308,7 @@ function validate (value = {}, schema = {}) {
...
@@ -155,7 +308,7 @@ function validate (value = {}, schema = {}) {
continue
continue
}
}
}
}
const
validateMethod
=
validator
[
type
]
const
validateMethod
=
this
.
validator
[
type
]
if
(
!
validateMethod
)
{
if
(
!
validateMethod
)
{
throw
new
Error
(
`invalid schema type:
${
type
}
`
)
throw
new
Error
(
`invalid schema type:
${
type
}
`
)
}
}
...
@@ -165,6 +318,7 @@ function validate (value = {}, schema = {}) {
...
@@ -165,6 +318,7 @@ function validate (value = {}, schema = {}) {
return
validateRes
return
validateRes
}
}
}
}
}
}
}
function
checkClientInfo
(
clientInfo
)
{
function
checkClientInfo
(
clientInfo
)
{
...
@@ -205,7 +359,7 @@ function checkClientInfo (clientInfo) {
...
@@ -205,7 +359,7 @@ function checkClientInfo (clientInfo) {
screenWidth
:
numberNotRequired
,
screenWidth
:
numberNotRequired
,
screenHeight
:
numberNotRequired
screenHeight
:
numberNotRequired
}
}
const
validateRes
=
validate
(
clientInfo
,
schema
)
const
validateRes
=
new
Validator
().
validate
(
clientInfo
,
schema
)
if
(
validateRes
)
{
if
(
validateRes
)
{
if
(
validateRes
.
errCode
===
ERROR
.
PARAM_REQUIRED
)
{
if
(
validateRes
.
errCode
===
ERROR
.
PARAM_REQUIRED
)
{
console
.
warn
(
'
- 如果使用HBuilderX运行本地云函数/云对象功能时出现此提示,请改为使用客户端调用本地云函数方式调试,或更新HBuilderX到3.4.12及以上版本。
\n
- 如果是缺少clientInfo.appId,请检查项目manifest.json内是否配置了DCloud AppId
'
)
console
.
warn
(
'
- 如果使用HBuilderX运行本地云函数/云对象功能时出现此提示,请改为使用客户端调用本地云函数方式调试,或更新HBuilderX到3.4.12及以上版本。
\n
- 如果是缺少clientInfo.appId,请检查项目manifest.json内是否配置了DCloud AppId
'
)
...
@@ -217,7 +371,6 @@ function checkClientInfo (clientInfo) {
...
@@ -217,7 +371,6 @@ function checkClientInfo (clientInfo) {
}
}
module
.
exports
=
{
module
.
exports
=
{
validate
,
Validator
,
validator
,
checkClientInfo
checkClientInfo
}
}
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/validator.test.js
0 → 100644
浏览文件 @
2faaec33
const
assert
=
require
(
'
assert
'
)
const
{
Validator
}
=
require
(
'
./validator
'
)
const
{
ERROR
}
=
require
(
'
./error
'
)
const
{
getType
}
=
require
(
'
./utils
'
)
const
testCaseList
=
[{
value
:
{
username
:
'
uname
'
},
schema
:
{
username
:
'
username
'
},
expected
:
undefined
},
{
value
:
{
username
:
'
123456
'
},
schema
:
{
username
:
'
username
'
},
expected
:
{
errCode
:
ERROR
.
INVALID_USERNAME
}
},
{
value
:
{
username
:
'
数字天堂
'
},
schema
:
{
username
:
'
username
'
},
expected
:
{
errCode
:
ERROR
.
INVALID_USERNAME
}
},
{
value
:
{
password
:
'
123456
'
},
schema
:
{
password
:
'
password
'
}
},
{
value
:
{
password
:
'
123456
'
},
schema
:
{
password
:
'
password
'
},
mixin
:
[{
type
:
'
password
'
,
handler
:
function
(
password
)
{
if
(
typeof
password
!==
'
string
'
||
password
.
length
<
10
)
{
return
{
errCode
:
ERROR
.
INVALID_PASSWORD
}
}
}
}],
expected
:
{
errCode
:
ERROR
.
INVALID_PASSWORD
}
}]
function
execTestCase
({
value
=
{},
schema
=
{},
mixin
=
[],
expected
=
undefined
,
error
=
''
}
=
{})
{
const
validator
=
new
Validator
()
for
(
let
i
=
0
;
i
<
mixin
.
length
;
i
++
)
{
const
{
type
,
handler
}
=
mixin
[
i
]
validator
.
mixin
(
type
,
handler
)
}
let
validateResult
,
validateError
try
{
validateResult
=
validator
.
validate
(
value
,
schema
)
}
catch
(
err
)
{
validateError
=
err
}
const
tag
=
JSON
.
stringify
({
value
,
schema
})
if
(
error
)
{
if
(
typeof
error
===
'
string
'
)
{
assert
.
strictEqual
(
validateError
.
message
.
indexOf
(
error
)
>
-
1
,
true
,
`[
${
tag
}
] error message error`
)
}
else
if
(
getType
(
error
)
===
'
regexp
'
)
{
assert
.
strictEqual
(
error
.
test
(
validateError
),
true
,
`[
${
tag
}
] error message error`
)
}
else
{
throw
new
Error
(
`[
${
tag
}
] invalid test case`
)
}
return
}
if
(
expected
===
undefined
)
{
assert
.
strictEqual
(
validateResult
,
undefined
,
`[
${
tag
}
] validate result error`
)
return
}
const
expectedKeys
=
Object
.
keys
(
expected
)
let
passResultCheck
=
true
for
(
let
i
=
0
;
i
<
expectedKeys
.
length
;
i
++
)
{
const
key
=
expectedKeys
[
i
]
if
(
expected
[
key
]
!==
validateResult
[
key
])
{
passResultCheck
=
false
break
}
}
assert
.
strictEqual
(
passResultCheck
,
true
,
`[
${
tag
}
] validate result error`
)
}
for
(
let
i
=
0
;
i
<
testCaseList
.
length
;
i
++
)
{
console
.
log
(
`test case:
${
i
}
`
)
execTestCase
(
testCaseList
[
i
])
console
.
log
(
`test case:
${
i
}
, pass`
)
}
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js
浏览文件 @
2faaec33
...
@@ -4,7 +4,8 @@ const {
...
@@ -4,7 +4,8 @@ const {
getType
getType
}
=
require
(
'
./common/utils
'
)
}
=
require
(
'
./common/utils
'
)
const
{
const
{
checkClientInfo
checkClientInfo
,
Validator
}
=
require
(
'
./common/validator
'
)
}
=
require
(
'
./common/validator
'
)
const
ConfigUtils
=
require
(
'
./lib/utils/config
'
)
const
ConfigUtils
=
require
(
'
./lib/utils/config
'
)
const
{
const
{
...
@@ -106,6 +107,46 @@ module.exports = {
...
@@ -106,6 +107,46 @@ module.exports = {
platform
:
clientPlatform
platform
:
clientPlatform
}).
getPlatformConfig
()
}).
getPlatformConfig
()
this
.
validator
=
new
Validator
()
/**
* 示例:覆盖密码验证规则
*/
// this.validator.mixin('password', function (password) {
// if (typeof password !== 'string' || password.length < 10) {
// // 调整为密码长度不能小于10
// return {
// errCode: ERROR.INVALID_PASSWORD
// }
// }
// })
/**
* 示例:新增验证规则
*/
// this.validator.mixin('timestamp', function (timestamp) {
// if (typeof timestamp !== 'number' || timestamp > Date.now()) {
// return {
// errCode: ERROR.INVALID_PARAM
// }
// }
// })
// // 新增规则同样可以在数组验证规则中使用
// this.validator.valdate({
// timestamp: 123456789
// }, {
// timestamp: 'timestamp'
// })
// this.validator.valdate({
// timestampList: [123456789, 123123123123]
// }, {
// timestampList: 'array<timestamp>'
// })
// // 甚至更复杂的写法
// this.validator.valdate({
// timestamp: [123456789, 123123123123]
// }, {
// timestamp: 'timestamp|array<timestamp>'
// })
// 挂载uni-captcha到this上,方便后续调用
// 挂载uni-captcha到this上,方便后续调用
this
.
uniCaptcha
=
uniCaptcha
this
.
uniCaptcha
=
uniCaptcha
...
...
uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/middleware/validate.js
浏览文件 @
2faaec33
const
{
validate
}
=
require
(
'
../common/validator
'
)
module
.
exports
=
function
(
value
=
{},
schema
=
{})
{
module
.
exports
=
function
(
value
=
{},
schema
=
{})
{
const
validateRes
=
validate
(
value
,
schema
)
const
validateRes
=
this
.
validator
.
validate
(
value
,
schema
)
if
(
validateRes
)
{
if
(
validateRes
)
{
delete
validateRes
.
schemaKey
delete
validateRes
.
schemaKey
throw
validateRes
throw
validateRes
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录