[英]Joi validator alternative schema error message
我試圖使用Joi.alternatives.try()創建一個 Joi 模式。 這是我嘗試過的架構。
Joi.alternatives().try(Joi.object({
type: Joi.number().required().label('Error1!!')
}), Joi.object({
reason: Joi.string().required().label('Error2!!')
})).label('Error!!')
這是我用過的object。
{ reason: 2 }
我期待錯誤為Error2!!
或包含此字符串Error2!!
. 但我得到錯誤
Validation Error: "Error!!" does not match any of the allowed types
此錯誤來自父節點。
如何使錯誤特定於 object? 即,來自備用 object 節點而不是父 object 節點的錯誤。
您可以使用此平台在線驗證架構。
更新:這是我使用的示例模式。
employee_retired = Joi.object({
type: Joi.number().required().valid(2, 3, 7),
reason: Joi.string().required()
.min(1)
.max(100),
firstname: Joi.string()
.required(),
lastname: Joi.string()
.required()
.min(1)
.max(255),
personaldetails: Joi.alternatives().conditional('type', {
is: 2, then: Joi.array().items(Joi.object({
address: Joi.string().required()
.min(1)
.max(100),
salary: Joi.string().required()
.min(0)
.max(500),
contactnumbers: Joi.array().items(Joi.object({
mobile: Joi.string().required()
.min(0)
.max(15),
home: Joi.string()
.required()
.min(1)
.max(15),
})).max(50).required(),
}).required()).max(50).required(),
otherwise: Joi.forbidden(),
}),
monthlysavings: Joi.alternatives().conditional('type', {
is: 3,
then: Joi.number()
.required()
.min(0)
.max(50000),
otherwise: Joi.forbidden(),
}),
isapproved: Joi.boolean().required(),
});
empolyee_working = Joi.object({
type: Joi.number().required().valid(2, 3, 7),
reason: Joi.string().required()
.min(1)
.max(100),
firstname: Joi.string()
.required(),
lastname: Joi.string()
.required()
.min(1)
.max(255),
contactnumbers: Joi.array().items(Joi.object({
mobile: Joi.string().required()
.min(0)
.max(15),
home: Joi.string()
.required()
.min(1)
.max(15),
})).max(50).required(),
monthlysavings: Joi.alternatives().conditional('type', {
is: 3,
then: Joi.number().required()
.min(1)
.max(50000),
otherwise: Joi.forbidden(),
}),
isapproved: Joi.boolean().required(),
})
const employee = Joi.alternatives().try(employee_retired, empolyee_working);
您可以使用object.or
進行此行為:
Joi.object({
type: Joi.number().label('Error1!!'),
reason: Joi.string().label('Error2!!')
}).or('type', 'reason').label('Error!!')
測試:
{}
// Validation Error: "Error!!" must contain at least one of [Error1!!, Error2!!]
{ reason: 2 }
// Validation Error: "Error2!!" must be a string
{ type: "a" } // note that due to default `convert` behavior, `{ type: "2" }` would pass
// Validation Error: "Error1!!" must be a number
{ a: "b" }
// Validation Error: "a" is not allowed. "Error!!" must contain at least one of [Error1!!, Error2!!]
確實它有點冗長,但遵循相同的邏輯:
personaldetails
詳細信息和contactnumbers
有所不同因此,以下內容應該為您提供精確的驗證錯誤消息(盡管我還沒有測試所有情況)。 我只是“合並”了兩個員工聲明,兩個不同的案例personaldetails
和contactnumbers
不再聲明為required
,而是在最終的or
中指定。
Joi.object({
type: Joi.number().required().valid(2, 3, 7),
reason: Joi.string().required()
.min(1)
.max(100),
firstname: Joi.string()
.required(),
lastname: Joi.string()
.required()
.min(1)
.max(255),
personaldetails: Joi.alternatives().conditional('type', {
is: 2, then: Joi.array().items(Joi.object({
address: Joi.string().required()
.min(1)
.max(100),
salary: Joi.string().required()
.min(0)
.max(500),
contactnumbers: Joi.array().items(Joi.object({
mobile: Joi.string().required()
.min(0)
.max(15),
home: Joi.string()
.required()
.min(1)
.max(15),
})).max(50).required(),
// N.B.: no more .required() on the next line, .or() will handle it conditionally
}).required()).max(50),
otherwise: Joi.forbidden(),
}),
contactnumbers: Joi.array().items(Joi.object({
mobile: Joi.string().required()
.min(0)
.max(15),
home: Joi.string()
.required()
.min(1)
.max(15),
// N.B.: no more .required() on the next line, .or() will handle it conditionally
})).max(50),
monthlysavings: Joi.alternatives().conditional('type', {
is: 3,
then: Joi.number()
.required()
.min(0)
.max(50000),
otherwise: Joi.forbidden(),
}),
isapproved: Joi.boolean().required(),
}).or('personaldetails', 'contactnumbers').label('OR failure')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.