为什么此 joi 正则表达式验证十六进制颜色失败
why is this joi regex validation for hex color failing
为什么这个正则表达式应该匹配但失败了?
错误
child "color" fails because ["color" with value "#333acf " fails to match the required pattern: /^#[A-Fa-f0-9]{6}$/]
validate: {
payload:{
avatar: joi.object().keys({
color: joi.string().regex(/^#[A-Fa-f0-9]{6}$/),
icon: joi.string().min(2)
})
}
}
有效载荷
{
"avatar": {color:"#333acf ",icon:"b1"}
}
您的颜色字符串末尾多了一个 space。应该是
{
"avatar": {color:"#333acf",icon:"b1"}
}
或者您的正则表达式需要通过删除字符串锚点的末尾来允许长度超过 6 个字符的字符串。
/^#[A-Fa-f0-9]{6}/
为什么这个正则表达式应该匹配但失败了?
错误
child "color" fails because ["color" with value "#333acf " fails to match the required pattern: /^#[A-Fa-f0-9]{6}$/]
validate: {
payload:{
avatar: joi.object().keys({
color: joi.string().regex(/^#[A-Fa-f0-9]{6}$/),
icon: joi.string().min(2)
})
}
}
有效载荷
{
"avatar": {color:"#333acf ",icon:"b1"}
}
您的颜色字符串末尾多了一个 space。应该是
{
"avatar": {color:"#333acf",icon:"b1"}
}
或者您的正则表达式需要通过删除字符串锚点的末尾来允许长度超过 6 个字符的字符串。
/^#[A-Fa-f0-9]{6}/