【问题标题】:Mongoose-Validators not validating isURL with expressMongoose-Validators 不使用 express 验证 isURL
【发布时间】:2017-11-30 02:37:52
【问题描述】:

我没有收到错误,即使当我没有输入有效的 url 时架构应该会失败。

我可以确认来自前端的数据正在正确发送。它正在通过路线传递给 Mongoose(如下所示)。

Hosts.Create(req.body, func...) 函数的回调没有 Err。

这是架构。验证器:[validators.isURL()] 没有产生消息。

var HostSchema = new Schema({
domain:  {
    type: String,
    required: [ true, 'A Domain is required' ],
 // This is the broken validator
    validator: [ validators.isURL({message: 'Must be a Valid URL', protocols: ['http','https','ftp'], require_tld: true, require_protocol: true} ) ]
},
pkg: {
    type: String,
    required: [ true, 'Hosting Package is required' ]
},
ssl: { type: Boolean, required: true },
maint: { type: Boolean, required: true },
...

});

我的路线文件:

// Process Add Cx
hosting.post('/add', function(req, res, next) {

// If No Request data.
if (req.body.constructor === Object && Object.keys(req.body).length === 0) {
    ... Send View if no form data ...

} else { // Proccess Data

    // Create new User
    Hosts.create(req.body, function(err, host) {
        if (err) return res.json({success: false, message: Hosts.MongoErrors(err)});
        // If everything was sucessful! Yay!
        res.json({success: true, message: 'Host Successfully Saved!'});
    }); 

}

【问题讨论】:

    标签: node.js mongodb validation mongoose


    【解决方案1】:

    这就是解决方案。

    domain:  {
        type: String,
        required: [ true, 'A Domain is required' ],
        validate: validators.isURL({message: 'Must be a Valid URL', protocols: ['http','https','ftp'], require_tld: true, require_protocol: true })
    },
    

    【讨论】:

      【解决方案2】:

      语法是这样的:

      validate: { 
        validator: value => validator.isURL(value, { protocols: ['http','https','ftp'], require_tld: true, require_protocol: true }),
        message: 'Must be a Valid URL' 
      }
      

      【讨论】:

      • 哈哈哈,谢谢!我相信这会帮助别人。文档不是很清楚。
      • “价值”从何而来?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 2012-01-13
      • 2016-05-05
      • 2019-09-01
      • 2016-10-31
      • 2013-12-26
      相关资源
      最近更新 更多