【发布时间】:2019-02-25 03:58:33
【问题描述】:
有一个对象有三个键
const abc = {
customerId: '777',
firstName: 'pqr'',
lastName: 'xyz',
};
条件是,如果存在客户 ID,则可以忽略名字和姓氏。否则,它们应该是最大长度为 20 的字符串。
const schema = Joi.object({
customerId: Joi.string(),
firstName: Joi.alternatives().when('customerId', {
is: null,
then: Joi.string(),
}),
lastName: Joi.alternatives().when('customerId', {
is: null,
then: Joi.string(),
})
})
Joi.validate(abc, schema);
这里出现这个错误
error: { ValidationError: "firstName" is not allowed 在 Object.exports.process (/home/runner/node_modules/joi/lib/errors.js:
那么,如何实现呢?
【问题讨论】:
标签: javascript json node.js validation joi