【发布时间】:2020-04-10 21:14:48
【问题描述】:
const schema = Joi.object().keys({
Id: Joi.number().required(),
CustomerName: Joi.string()
.trim()
.required()
.when('$isInValidCustomer', {
is: true,
then: //Add some error in existing error block,
}),
BankName: Joi.string().trim(),
});
const custDetail = {
Id: 2,
CustomerName: 'xyz'
BankName: ''
};
const schemaOptions = {
abortEarly: false,
context: {
isInValidCustomer: true,
},
};
const valError = schema.validate(custDetail, schemaOptions);
所以,现在当我验证“custDetail”对象时,我想要以下 2 个错误: - CustomerName 错误,因为 'isInValidCustomer' 为真 - 需要银行名称
我无法在现有错误对象中附加 CustomerName 的错误。如果我使用“.error()”,那么只会得到与“CustomerName”相对应的单个错误,否则只会得到 BankName 的错误。
非常感谢任何帮助。
【问题讨论】:
标签: javascript node.js validation hapijs joi