Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mongoose自定义验证在控制器中不工作_Javascript_Node.js_Mongodb_Mongoose - Fatal编程技术网

Javascript Mongoose自定义验证在控制器中不工作

Javascript Mongoose自定义验证在控制器中不工作,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我的mongoose模型包含一个字段,该字段仅在另一个字段等于特定值时才是必需的(即,它是有条件的) 在本例中,我有一个itemType为“typeA”或“typeB”的项。字段someField仅对“typeB”是必需的 在我的测试中,当直接针对模型进行测试时,验证似乎有效。但是,验证不会在控制器中触发 我的模型如下: var mongoose = require('mongoose'), Schema = mongoose.Schema; var ItemSchema = new Sc

我的mongoose模型包含一个字段,该字段仅在另一个字段等于特定值时才是必需的(即,它是有条件的)

在本例中,我有一个itemType为“typeA”或“typeB”的项。字段someField仅对“typeB”是必需的

在我的测试中,当直接针对模型进行测试时,验证似乎有效。但是,验证不会在控制器中触发

我的模型如下:

var mongoose = require('mongoose'),
  Schema = mongoose.Schema;

var ItemSchema = new Schema({
  name: {
    type: String,
    trim: true,
    required: true
  },
  itemType: {
    type: String,
    enum: ['typeA', 'typeB'],
    required: true
  },
  someField: String
});

ItemSchema
  .path('someField')
  .validate(function(value, respond) {
    if (this.itemType === 'typeA') { return respond(true); }
    return respond(validatePresenceOf(value));
  }, 'someField cannot be blank for typeB');

function validatePresenceOf(value) {
  return value && value.length;
}

module.exports = mongoose.model('Item', ItemSchema);
在我的模型单元测试中:

it('should fail when saving typeB without someField', function(done) {

  var item = new Item({
    name: 'test',
    itemType: 'typeB'
  });

  item.save(function(err){
    should.exist(err);
    done();
  });

});
上述单元测试工作正常。但是,在测试API本身时,Mongoose不会引发错误。如果控制器无法保存,则应返回500错误:

exports.create = function(req, res) {
  var item = new Item(req.body);
  item.save(function(err, data) {
    if (err) { return res.json(500, err); }
    return res.json(200, data);
  });
};
但是,以下测试始终返回200:

var request = require('supertest');

describe('with invalid fields', function() {
  it('should respond with a 500 error', function(done) {
    request(app)
      .post('/api/item')
      .send({
        name: 'test',
        itemType: 'typeB'
        })
      .expect(500)
      .end(function(err, res) {
        if (err) return done(err);
        return done();
        });
      });
  });
});

我不确定我做错了什么,当我保存在控制器中时,似乎没有触发Mongoose验证。

这里的实现方式是错误的。您不会对“someField”进行验证,而是对传递给“itemType”的值进行验证。原因是,由于您没有为“someField”提供任何值,因此不会调用验证程序,因为没有定义任何内容

因此,测试以另一种方式运行,并更正您的
validatePresenceOf()
函数:

itemSchema.path('itemType').validate(function(value) {
  if ( value === 'typeA' )
    return true;
  console.log( validatePresenceOf(this.someField) );
  return validatePresenceOf(this.someField);

}, 'someField cannot be blank for itemType: "typeB"');

function validatePresenceOf(value) {
  if ( value != undefined )
    return value && value.length
  else
    return false;
}

如果“itemType”设置为“typeB”,而“someField”没有任何值,则会正确抛出错误。

谢谢Neil,这很有意义:)