在哪里验证在Express.js项目 - 验证数据库层(重新Mongoose)? [英] Where to validate in Express.js project – Validation at the database layer (re. Mongoose)?

查看:122
本文介绍了在哪里验证在Express.js项目 - 验证数据库层(重新Mongoose)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Express.js中写一个表单的应用程序,首先,我正在路由中进行所有的验证(或者控制器,如果你愿意的话):

  app.post('/ register',function(req,res,next){
// Generic validation
req.assert('name ','Name is empty')。notEmpty();
req.assert('username','Username is empty')。notEmpty();
var errors = req.validationErrors(true)
如果(错误){
//如果有错误,显示
} else {
//如果没有错误,使用模型保存到数据库
}
});

但是,我很快了解到,我的验证应该在模型中发生,保持与薄控制器,脂肪模型原则。



型号:

  var userSchema = new Schema({
name:{
type:String
,required:true
,validate:[validators.notEmpty,'Name is empty']
}
,username:{
type:String
,required:true
,validate:[validators.notEmpty,'Username is empty']
}
,salt:String
,hash:String
});

路由/控制器:

  app.post('/ register',function(req,res,next){
var newUser = new User(req.body);
//告诉模型尝试将数据保存到数据库
newUser.save(function(err){
if(err){
//有验证错误
} else {
//没有错误
}
});
});

效果很好。但是,我需要在数据库层之前进行验证。例如,我需要检查两个密码是否相同密码 confirmPassword )。这不能在模式中定义,因为我只在模型中保存 salt hash 。因此,我需要在数据库层之前进行此验证,在路由/控制器中。因此,我将无法一起显示验证消息。



这是做事情的最佳方式 - 在数据库层的模型中验证,如和控制器一样?在控制器中进行所有验证是否比以前更好?但是后来我会重复代码,我再次保存到模型中。或者我应该使用另一种模式,如果是,那么什么?

解决方案

我会考虑将验证逻辑移到模型,但停止将模型视为数据库。该模型大于数据库。模型执行验证,如果验证通过,将数据保存到数据库,如果验证失败,则返回正确的消息,以便路由器可以呈现正确的错误消息。


I am writing an application with a form in Express.js, and to begin with, I was doing all of my validation in the route (or controller, if you like):

app.post('/register', function (req, res, next) {
  // Generic validation
  req.assert('name', 'Name is empty').notEmpty();
  req.assert('username', 'Username is empty').notEmpty();
  var errors = req.validationErrors(true);
  if (errors) {
    // If there are errors, show them
  } else {
    // If there are no errors, use the model to save to the database
  }
});

However, I quickly learnt that my validation should be happening in the model, keeping inline with the "thin controller, fat model" principle.

Model:

var userSchema = new Schema({
    name: {
      type: String
    , required: true
    , validate: [validators.notEmpty, 'Name is empty']
    }
  , username: {
      type: String
    , required: true
    , validate: [validators.notEmpty, 'Username is empty']
    }
 , salt: String
 , hash: String
});

Route/controller:

app.post('/register', function (req, res, next) {
  var newUser = new User(req.body);
  // Tell the model to try to save the data to the database
  newUser.save(function (err) {
    if (err) {
      // There were validation errors
    } else {
      // No errors
    }
  });
});

This works well. However, I need to do validation prior to the database layer. For instance, I need to check if two passwords are the same (password and confirmPassword). This cannot be defined in the schema, as I am only saving salt and hash in the model. Therefore, I need to do this validation before the database layer, in the route/controller. Because of this, I will not be able to display validation messages together.

Is this the best way thing to do things – validating in the model at the database layer, as well as in the controller? Is it better to have all of my validation in the controller like before? But then I will be repeating code where I save to the model again. Or should I use another pattern, and if so, what?

解决方案

I would consider moving the validation logic to the Model but stop thinking of the Model as the database. The model is greater than the database. The model performs the validation, and if the validation passes it saves the data to the database, if the validation fails it returns the correct messages so that a router can render the proper error messages.

这篇关于在哪里验证在Express.js项目 - 验证数据库层(重新Mongoose)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆