![](/img/trans.png)
[英]Joi validate array of objects, such that only one object has specific property
[英]Joi - validate property against value in array of objects
我正在尝试根据定义的对象数组验证有效负载中特定属性的值。
例如
有效载荷
{
a:[1, 2]
}
“ a”的值必须是在对象数组中定义的id之一(允许多个值)
[
{
"id":1,
"share":{
"x":100,
"y":0,
"z":0
}
},
{
"id":2,
"share":{
"x":90,
"y":0,
"z":10
}
}
....and so on
]
您能否帮助建议Joi是否可以实现这一目标?
谢谢,Gowrish
乔的array.validate()与项目应该做你想要的。
const Joi = require("joi")
const input = { a: [ 1, 2 ] }
const objects = [{ id: 1 }, { id: 2 }, { id: 3 }]
const schema = {
a: Joi.array().items(Joi.any().valid(objects.map(o => o.id)))
}
const result = Joi.validate(input, schema, (err, result) => {
if (err) {
console.error('error:', err)
return
}
console.log('result:', result)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.