gpt4 book ai didi

javascript - Joi 验证器替代模式错误消息

转载 作者:行者123 更新时间:2023-12-04 10:08:30 28 4
gpt4 key购买 nike

我尝试使用 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!!')

这是我用过的对象。

{ reason: 2 }

我期待错误为 Error2!! 或包含字符串 Error2!! 的内容。但是我收到错误

验证错误:“错误!!”不匹配任何允许的类型

这个错误来自父 Node 。

如何使错误特定于对象?即,来自替代对象 Node 而不是父对象的错误。

您可以使用 this在线验证模式的平台。

更新:这是我使用的示例架构。

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!!]

更新(评论后)

确实有点冗长,但遵循相同的逻辑:

  • 退休工作员工共享相同的结构
  • 只有personaldetailscontactnumbers变化

因此,以下几行内容应该会为您提供准确的验证错误消息(尽管我还没有测试所有情况)。我只是“合并”了员工声明和两个不同的案例 personaldetailscontactnumbers未声明 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')

关于javascript - Joi 验证器替代模式错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61453327/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com