【发布时间】:2021-04-29 18:58:44
【问题描述】:
我在我的 Joi 架构中创建了一个自定义函数来验证名称是否为 equal 到 confirmName。
我使用abortEarly: false 来获取所有错误。
问题是当object 中的验证失败时custom 函数没有调用。
目前结果输出仅适用于birthYear。应该是birthYear 和custom。
有没有办法让它像我描述的那样工作?
{ name: "foo", confirmName: "oo", birthYear: 0 },
const Joi = require("joi");
console.clear();
const schema = Joi.object({
name: Joi.string(),
confirmName: Joi.string(),
birthYear: Joi.number().integer().min(1900).max(2013)
}).custom((doc, helpers) => {
const { name, confirmName } = doc;
if (name !== confirmName) {
throw new Error("name not match!!");
}
});
const { error } = schema.validate(
{ name: "foo", confirmName: "oo", birthYear: 0 },
{ allowUnknown: true, abortEarly: false }
);
console.log({ error });
if (error) {
const { details } = error;
console.log({ details });
}
【问题讨论】:
标签: javascript joi