![](/img/trans.png)
[英]Validate that two keys of object should have same value using Joi?
[英]Node Js Joi validate object that contains list of same object
假設我有以下以 json 表示的對象:
{
"name": "Joe",
"childrens": [
{
"name": "Marie",
"childrens": [
{
"name": "Paul"
}
]
},
{
"name": "Nick",
"childrens": [
]
}
]
}
如何創建模式來驗證包含同一對象列表的對象?
您可以使用Joi.lazy()
方法來實現遞歸模式,例如:
const Person = Joi.object({
name: joi.string().required(),
children: joi.array().items(joi.lazy(() => Person).description('My Schema'))
});
在較新版本的 Joi 中,您必須使用Joi.link()
來鏈接模式的id
。
const personSchema = joi
.object({
name: joi.string().required(),
children: joi.array().items(joi.link("#person"))
})
.id("person");
請注意,我將您的childrens
屬性重命名為children
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.