我有一个类似
的对象{
"a": {
"name": "name of a",
"year": "1"
},
"b": {
"name": "name of b",
"year": "2"
},
"c": {
"name": "name of c",
"year": "123456"
}
}
我正在使用以下验证
var mainobject=Joi.object().pattern(/^/,subObject),
var suboject=joi.object({
name:joi.string(),
year:joi.string()
})
使用此代码,我可以遍历对象吗?我的第一个代码有什么问题吗?在主要对象中,我将模式用于未知键。如果我想再添加一个数据,例如详细信息,
{
"a": {
"name": "name of a",
"year": "1"
"details":(should include name and year in "name":'name of a',"year":'1')
},
"b": {
"name": "name of b",
"year": "2"
},
"c": {
"name": "name of c",
"year": "123456"
}
}
如何实现上述解决方案?请帮帮我。
答案 0 :(得分:0)
使用.unknown()
API允许未知密钥。
var subobject = Joi.object({
name:Joi.string(),
year:Joi.string()
}).unknown();