【发布时间】:2021-12-23 10:50:42
【问题描述】:
如何验证以下两个参考的总数?我希望custom 在total 上,但不确定在使用Joi.ref 时如何在自定义中获取refs 的值。
我可以执行以下操作并将 custom 从整个验证架构中挂起,但更愿意将其附加到 total。
我不想使用expression()
我不希望按照this answer 更改架构结构。
const widgetValidator = Joi.object({
a: Joi.number().integer().min(0).required(),
b: Joi.number().integer().min(1).required(),
total: Joi.number().integer().min(1).required(),
}).custom((value: {a: number; b: number; total: number;}, helpers) => {
if (value.total !== value.a + value.b) {
throw new Error('invalid balance calculation');
}
return value;
},
);
【问题讨论】:
标签: javascript typescript joi