Joi - 根据对象数组中的值验证 属性
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
Joi 的 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)
})
我正在尝试根据定义的对象数组验证负载中特定 属性 的值。
例如
有效负载
{
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
Joi 的 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)
})