Joi验证正则表达式或模式 [英] Joi Validation Regex or pattern
本文介绍了Joi验证正则表达式或模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想让joi使用在变量中定义的正则表达式模式
I want to joi use regex pattern which define in variable
我有一个变量 pattern ,其中包含正则表达式即
I have a variable pattern which contains regex i.e
pattern = "/^[0-9+]{7}-[0-9+]{1}$/"
此模式发送到Joi模块并要确认
and this pattern send to Joi module and want to confirm
module.exports = {
save: {
body: {
match: Joi.string().regex(pattern).required
}
}
}
如果使用此功能,我知道验证工作
I know validation work if I use this
module.exports = {
save: {
body: {
match: Joi.string().regex(/^[0-9+]{7}-[0-9+]{1}$/).required
}
}
}
但是就我而言,每次正则表达式都会不同.所以我不能使用上面的正则表达式模式
But in my case every time regex will different. So I can not use above regex pattern
推荐答案
如果要将模式用作变量,只需将其传递:
If you want to use pattern as variable, just pass it:
module.exports = (pattern) => ({
save: {
body: {
match: Joi.string().regex(pattern).required
}
}
});
并像这样使用它:
const pattern = "/^[0-9+]{7}-[0-9+]{1}$/";
validator(pattern)
这篇关于Joi验证正则表达式或模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文