Joi对象验证:如何验证具有未知键名的值? [英] Joi object validation: How to validate values with unknown key names?
问题描述
我有一个对象,它的键名我可能不知道-它们是由用户创建的.但是我确实知道它们(键)将存储什么值,它们(值)将是ISO字符串.如何验证这些值?而且,可选,如何验证未知对象的密钥,即:
I have an object with key names I cannot possibly know - they are created by user. However I do know what values they (keys) are going to store, and they (values) are going to be ISO strings. How do I validate those values? And, optionally, how do I validate uknown object's keys, i.e.:
key: Joi.string().min(2).max(25)
我已经尝试过的基于Joi API文档:
What I have already tried was based on Joi API docs :
使用Joi.object([schema])代替普通的JS对象的另一个好处是>您可以在对象上设置任何选项,例如允许未知键,例如:
const schema = Joi.object({
arg: Joi.string().valid('firstname', 'lastname', 'title', 'company', 'jobtitle'),
value: Joi.string(),
}).pattern(/firstname|lastname/, Joi.string().min(2));
const schema = Joi.object({
arg: Joi.string().valid('firstname', 'lastname', 'title', 'company', 'jobtitle'),
value: Joi.string(),
}).pattern(/firstname|lastname/, Joi.string().min(2));
我从示例中了解到,arg
键代表Joi.object()
的key
,而value
代表它的value
.
What I understood from the example is that arg
key represents Joi.object()
's key
, and value
represents it's value
.
我的示例:
campaign: Joi.object({
arg: Joi.string().valid( 'unknown' ),
value: Joi.date().iso(),
}).pattern( /unknown/, Joi.string().min(2).max(25) )
我的输入;
campaign: { g_ad_adwords: "2017-01-19T11:33:26.205Z" }
我的错误:
广告系列"失败,因为[[g_ad_adwords]不允许使用
"campaign" fails because ["g_ad_adwords" is not allowed]
推荐答案
尝试一下.它基本上会接受对象campaign
中的任何键,并且该值必须针对Joi.date().iso()
Try this. It'll basically accept any key within an object campaign
and the value must validate against Joi.date().iso()
campaign: Joi.object().pattern(/^/, Joi.date().iso())
这将与任何键匹配.您可以通过稍微填充正则表达式来限制它.例如只有2到25个字符的单词字符
This however will match any key. You can restrict this by padding out the regex a little. e.g. only word characters between 2 and 25 chars
campaign: Joi.object().pattern(/\w{2,25}/, Joi.date().iso())
更新
关于Joi文档中的示例,我没有测试过,但这是我的解释.我可以理解,这不是他们所能给出的最直接的例子……
Regarding the example in the Joi docs, I haven't tested it but here's my interpretation. I can understand that it's not the most straightforward example they could have given...
const schema = Joi.object({
arg: Joi.string().valid('firstname', 'lastname', 'title', 'company', 'jobtitle'),
value: Joi.string(),
}).pattern(/firstname|lastname/, Joi.string().min(2));
要验证的对象必须包含两个属性arg
和value
,其中arg
的值可以是'firstname', 'lastname', 'title', 'company', 'jobtitle'
之一,而value
只是一个字符串. /p>
The objects to validate must contain the two attributes arg
and value
where arg
's value can be one of 'firstname', 'lastname', 'title', 'company', 'jobtitle'
and value
is just a string.
{
arg: 'firstname',
value: 'john'
}
{
arg: 'lastname',
value: 'smith'
}
{
arg: 'jobtitle',
value: 'brewer'
}
但是,它还将允许对象具有属性firstname
和lastname
,其中它们的两个值都是具有两个以上字符的字符串.因此,以上示例可以简化为一个有效的对象.
However it will also allow the object to have the attributes firstname
and lastname
where both of their values is a string with more than two characters. So the above examples could be condensed into a single valid object.
{
firstname: 'john',
lastname: 'smith',
arg: 'jobtitle',
value: 'brewer'
}
这篇关于Joi对象验证:如何验证具有未知键名的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!