改善猫鼬验证错误处理

时间:2020-04-06 08:46:51

标签: node.js validation mongoose error-handling ejs

我具有以下具有所需验证的架构:

var mongoose = require("mongoose");
var validator = require("validator");

var userSchema = new mongoose.Schema(
  {
    email: {
      type: String,
      required: [true, "Email is a required field"],
      trim: true,
      lowercase: true,
      unique: true,
      validate(value) {
        if (!validator.isEmail(value)) {
          throw new Error("Please enter a valid E-mail!");
        }
      },
    },
    password: {
      type: String,
      required: [true, "Password is a required field"],
      validate(value) {
        if (!validator.isLength(value, { min: 6, max: 1000 })) {
          throw Error("Length of the password should be between 6-1000");
        }

        if (value.toLowerCase().includes("password")) {
          throw Error(
            'The password should not contain the keyword "password"!'
          );
        }
      },
    },
  },
  {
    timestamps: true,
  }
);

var User = mongoose.model('User', userSchema);

我通过以下方式发送发帖请求,从而通过表格传递电子邮件和密码:

router.post("/user", async (req, res) => {
  try {
    var user = new User(req.body);
    await user.save();
    res.status(200).send(user);
  } catch (error) {
    res.status(400).send(error);
  }
});

module.exports = mongoose.model("User", user);

每当我输入一个违反验证规则的字段时,就会收到很长的错误消息,这很明显。但是现在,我想改进错误处理,以便为用户轻松解释。而不是重定向到通用错误页面,如何重定向到相同的注册页面,并在显示错误的错误字段附近显示Flash消息?同样,如果成功,也应该执行类似的操作,例如顶部的绿色闪烁消息。

我在注册页面上使用ejs。

3 个答案:

答案 0 :(得分:0)

在catch块中,您可以检查错误是否为猫鼬验证错误,并动态创建一个错误对象,如下所示:

router.post("/user", async (req, res) => {
  try {
    var user = new User(req.body);
    await user.save();
    res.status(200).send(user);
  } catch (error) {
    if (error.name === "ValidationError") {
      let errors = {};

      Object.keys(error.errors).forEach((key) => {
        errors[key] = error.errors[key].message;
      });

      return res.status(400).send(errors);
    }
    res.status(500).send("Something went wrong");
  }
});

当我们发送这样的请求正文时:

{
   "email": "test",
   "password": "abc"
}

响应将是:

{
    "email": "Please enter a valid E-mail!",
    "password": "Length of the password should be between 6-1000"
}

答案 1 :(得分:0)

您可以像这样使用validator而不是抛出错误:

password:{
    type:String,
    required:[true, "Password is a required field"],

validate: {
  validator: validator.isLength(value,{min:6,max:1000}),
  message: "Length of the password should be between 6-1000"
}
    }

答案 2 :(得分:0)

您可以发送

res.status(400).send(error.message);

而不是:

res.status(400).send(error);

,您还应该在Schema中进行一些更改。

validate(value){
            if (!validator.isEmail(value)) {
                throw new Error("Please enter a valid E-mail!");
            }
        }

validate: [validator.isEmail, "Please enter a valid E-mail!" ]

并输入密码:

minlength: 6,
maxlength:1000,
validate:{
validator: function(el){
return el.toLowerCase() !== "password"
}
message: 'The password should not contain the keyword "password"!'
}