在 Joi 中返回多个错误

Returning multiple errors in Joi

我正在尝试 return 来自我的 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: Cannot read 属性 'filter' of undefined 这看起来是由

引起的
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")

我无法弄清楚出了什么问题,而且我无法通过任何其他方式return使多个自定义错误消息起作用

你可以试试这样的

email: emailSchema.required().error(() => {
    return {
      message: "Email is required."
    };
}),

错误

我调试了您的代码,只是 returning () => 'some error message' 不适用于您的解决方案。我们需要return一个函数。您收到错误消息是因为您在自定义错误消息中的 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
                  }
                }
              }
            }),
});


辅助函数

我的解决方案的核心是以下功能。它 return 是一个 function,其中 return 是一个自定义错误 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. I noticed that your confirmPassword property is not required!

看到这仍然有很多观点,我想补充一点,这就是我目前使用 Joi 处理多个错误的方式。我只是将我所有的 validation/sanitization 函数串在一起,然后没有使用 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.",
  }),
});