我正在尝试从Joi验证模式返回多个自定义错误消息。这是架构
const Joi = require("@hapi/joi");
const string = Joi.string();
const emailSchema = string.email();
const usernameSchema = string
.min(3)
.max(30)
.error(() => "Username must be between 3 and 30 characters");
const passwordSchema = string
.min(6)
.error(() => "Password must be at least 6 characters");
const confirmPasswordSchema = Joi.valid(Joi.ref("passwordSchema")).error(
() => "Passwords must match"
);
const localRegistrationSchema = Joi.object().keys({
email: emailSchema.required().error(() => "Email is required"),
username: usernameSchema.required().error(() => "Username is required"),
password: passwordSchema.required().error(() => "Password is required"),
confirmPassword: confirmPasswordSchema
});
这是我使用模式的地方
const { error } = localRegistrationSchema.validate(req.body, {
abortEarly: false
});
console.log(error);
if (error) throw Boom.boomify(error);
但我一直收到TypeError:无法读取未定义的属性“ filter”,该属性似乎是由
引起的details.push({
message,
path: item.path.filter((v) => typeof v !== 'object'),
type: item.code,
context: item.local
});
这是Joi错误处理代码的一部分
当我不附加.error()部分时,我没有收到此错误,但是如果使用.error(new Error(“ custom error message”)),我将收到一个以上的错误信息来显示
我无法弄清楚出了什么问题,也无法获得其他方法来返回多个自定义错误消息
答案 0 :(得分:3)
我调试了您的代码,仅返回() => 'some error message'
对您的解决方案无效。我们需要返回一个函数。您收到错误消息是因为自定义错误消息的path
属性是undefined
。
const schema = Joi.object({
prop: Joi.string()
.min(9)
.error(() => 'min error message')
.required()
.error(() => 'required error message');
});
const schema = Joi.object({
username: Joi.string()
.min(9)
.required()
.error((errors) => {
for (err of errors) {
switch (err.code) {
case ('string.min'): {
return simpleErrorMsgFunc("prop min error message", ["prop"])(); // invoke
}
case 'any.required': {
return simpleErrorMsgFunc("prop is required", ["prop"])(); // invoke
}
default: {
return simpleErrorMsgFunc("prop has error", ["prop"])(); // invoke
}
}
}
}),
});
我的解决方案的核心是以下功能。它返回一个function
,它返回一个自定义错误object
。:
function simpleErrorMsgFunc(message, path) {
return () => {
return {
toString: () => message,
message,
path,
}
};
}
const Joi = require("@hapi/joi");
function simpleErrorMsgFunc(message, path) {
return () => {
return {
toString: () => message,
message,
path,
}
};
}
const localRegistrationSchema = Joi.object().keys({
// email is simple, we only need 1 error message
email: Joi.string()
.email()
.required()
.error(simpleErrorMsgFunc("Email is required", ["email"])),
// username is advanced, we need 2 error message
username: Joi.string()
.min(3)
.max(30)
.required()
.error((errors) => {
for (err of errors) {
switch (err.code) {
case ('string.min' || 'string.max'): {
return simpleErrorMsgFunc("username must be between 3 and 30 characters", ["username"])(); // invoke
}
case 'any.required': {
return simpleErrorMsgFunc("username is required", ["username"])(); // invoke
}
default: {
return simpleErrorMsgFunc("username has error", ["username"])(); // invoke
}
}
}
}),
// password is advanced, we need 2 error message
password: Joi.string()
.min(6)
.required()
.error((errors) => {
for (err of errors) {
switch (err.code) {
case ('string.min'): {
return simpleErrorMsgFunc("Password must be at least 6 characters", ["password"])(); // invoke
}
case 'any.required': {
return simpleErrorMsgFunc("Password is required", ["password"])(); // invoke
}
default: {
return simpleErrorMsgFunc("password has error", ["password"])(); // invoke
}
}
}
}),
confirmPassword: Joi.valid(Joi.ref("password"))
.error(simpleErrorMsgFunc("Passwords must match", ['confirmPassword']))
});
const req = {
body: {
email: 'some@gmail.com',
username: 'hee',
password: '45645656',
confirmPassword: '45645656_',
},
};
const { error } = localRegistrationSchema.validate(req.body, {
abortEarly: false
});
console.log(JSON.stringify(error, null, 2));
P.S。我注意到您的
confirmPassword
属性不是必需的!
答案 1 :(得分:1)
看到仍然有很多意见,我想补充一点,这就是我目前如何处理Joi的多个错误。我只是将所有验证/清除功能串在一起,然后不使用switch语句,而是将所有自定义消息都以.messages({})结尾。如果您在前端使用yup,则同样适用。这比switch语句和错误消息帮助程序功能更加简洁明了。
const string = Joi.string();
const passPattern =
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*\\W)[a-zA-Z0-9\\S]{8,}$";
export const signupLocalSchema = Joi.object({
email: string.email().trim().lowercase().required().messages({
"string.email": "Not a valid email address.",
"string.empty": "Email is required.",
}),
username: string.min(3).max(30).trim().lowercase().required().messages({
"string.min": "Username must be between 3 and 30 characters.",
"string.max": "Username must be between 3 and 30 characters.",
"string.empty": "Username is required.",
}),
password: string.pattern(new RegExp(passPattern)).messages({
"string.pattern.base":
"Password must be at least 8 characters and contain at least 1 lowercase, 1 uppercase, 1 number and 1 special character.",
}),
confirmPassword: Joi.valid(Joi.ref("password")).messages({
"any.only": "Passwords must match.",
}),
});
答案 2 :(得分:0)
您可以尝试这样的事情
email: emailSchema.required().error(() => {
return {
message: "Email is required."
};
}),