![](/img/trans.png)
[英]How to check if an object's properties represent a super set of another object?
[英]How to check if two properties are equal to another object's properties in Joi?
是否可以讓Joi
檢查source.listId === destination.listId && source.index === destination.index
然后返回錯誤?
我的例子:
const schema = Joi.object({
source: Joi.object().keys({
listId: Joi.string()
.length(24)
.required()
.when('...destination.index', {
is: Joi.equal(Joi.ref('index')),
then: Joi.disallow(Joi.ref('...destination.listId'))
}),
index: Joi.number()
.integer()
.positive()
.required()
}),
destination: Joi.object().keys({
listId: Joi.string()
.length(24)
.required(),
index: Joi.number()
.integer()
.positive()
.required()
})
});
解決了
我還必須使用when
驗證source.index
屬性:
const schema = Joi.object({
source: Joi.object().keys({
listId: Joi.string()
.length(24)
.required()
.when('...destination.index', {
is: Joi.equal(Joi.ref('index')),
then: Joi.disallow(Joi.ref('...destination.listId'))
}),
index: Joi.number()
.integer()
.positive()
.required()
.when('...destination.listId', {
is: Joi.equal(Joi.ref('listId')),
then: Joi.disallow(Joi.ref('...destination.index'))
})
}),
destination: Joi.object().keys({
listId: Joi.string()
.length(24)
.required(),
index: Joi.number()
.integer()
.positive()
.required()
})
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.