Node.js 如何在模式中的mongoose子元素上添加验证?

Node.js 如何在模式中的mongoose子元素上添加验证?,node.js,validation,mongoose,Node.js,Validation,Mongoose,很抱歉,这是显而易见的,但我在谷歌上搜索了几个小时,大多数结果都与子文档/嵌套模式有关,这是针对对象数组的,而不是我的案例 为了简单起见,我只构造了一个最小对象,但概念是一样的 我有一个mongoose模式,如下所示,我想通过validateFunction验证父对象,validateFunction只是检查firstName/lastName是否存在: var PersonSchema = new Schema({ firstName : String, lastName : Stri

很抱歉,这是显而易见的,但我在谷歌上搜索了几个小时,大多数结果都与子文档/嵌套模式有关,这是针对对象数组的,而不是我的案例

为了简单起见,我只构造了一个最小对象,但概念是一样的

我有一个mongoose模式,如下所示,我想通过validateFunction验证父对象,validateFunction只是检查firstName/lastName是否存在:

var PersonSchema = new Schema({
  firstName : String,
  lastName : String, 
  father : {
    firstName: String,
    lastName: String
 }, 
  mother : {
    firstName: String,
    lastName: String
 }
};
我试过了

var PersonSchema = new Schema({
  firstName : String,
  lastName : String, 
  father : {
    type: {
      firstName: String,
      lastName: String
    },
    validate : validateFunction
  }, 
  mother : {
    firstName: String,
    lastName: String
  }
};
这似乎是可行的,但阅读后,似乎该类型在mongoose中不是有效的类型

有人能告诉我在子对象(父对象)上添加验证的正确方法吗

注意:我已经检查了这一点,这是类似的,但我不想在单独的集合中存储“父亲”,因为人是唯一使用“父亲”的对象,所以我认为父亲应该在“人”对象中

编辑:我用以下代码测试了@Azem建议:

var log = function (val) {
    console.log(val);
    return true ;
}
var validateFunction = function (val) {
    if (typeof val === 'undefined') {
        return false;
    }
    console.log(typeof val, val);
    return true;
}
var many = [
    { validator: log, msg: 'fail to log' },
    { validator: validateFunction, msg: 'object is undefined' }
];
var PersonSchema = new Schema({
  firstName : String,
  lastName : String, 
  father : {
    firstName: String, 
    lastName: {type : String }
    validate : many // following http://mongoosejs.com/docs/api.html#schematype_SchemaType-validate
  }, 
    mother : {
    firstName: String,
    lastName: String
     }
  });

var PersonModel = mongoose.model("PersonTest", PersonSchema);

var josephus = new PersonModel({firstName:'Josephus', father:{lastName:null}});
josephus.save(function(error) {
    console.log("testing", error);
})
然后出错了

***/index.js:34
    validate : many
    ^^^^^^^^
SyntaxError: Unexpected identifier
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:945:3
如果模式更改为以下模式,它将正常工作(验证函数是否正常运行)


我举了一个例子,在我的moongoose模式中加入了一些小的验证,希望能有所帮助

var UserType = require('../defines/userType');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

//Schema for User
var UserSchema = new Schema({
     name: {
          type: String,
          required: true
     },
     email: {
          type: String
     },
     password: {
          type: String,
          required: true
     },
     dob: {
          type: Date,
          required: true
     },
     gender: {
          type: String, // Male/Female
          required: true
          default: 'Male'
     },
     type: {
         type: Number,
         default: UserType.User
     },

    address:{
        streetAddress:{
             type: String,
             required: true
        },
        area:{
             type: String
        },
        city:{
             type: String,
             required: true
        },
        state:{
             type: String,
             required: true
        },
        pincode:{
             type: String,
             required: true
        },
    },
    lastLocation: {
        type: [Number], // [<longitude>, <latitude>]
        index: '2d',    // create the geospatial index
        default: [77.2166672, 28.6314512]
    },
    lastLoginDate: {
        type: Date,
        default: Date.now
    },

});

//Define the model for User
var User;
if(mongoose.models.User)
    User = mongoose.model('User');
else
    User = mongoose.model('User', UserSchema);

//Export the User Model
module.exports = User;

我举了一个例子,在我的moongoose模式中加入了一些小的验证,希望能有所帮助

var UserType = require('../defines/userType');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

//Schema for User
var UserSchema = new Schema({
     name: {
          type: String,
          required: true
     },
     email: {
          type: String
     },
     password: {
          type: String,
          required: true
     },
     dob: {
          type: Date,
          required: true
     },
     gender: {
          type: String, // Male/Female
          required: true
          default: 'Male'
     },
     type: {
         type: Number,
         default: UserType.User
     },

    address:{
        streetAddress:{
             type: String,
             required: true
        },
        area:{
             type: String
        },
        city:{
             type: String,
             required: true
        },
        state:{
             type: String,
             required: true
        },
        pincode:{
             type: String,
             required: true
        },
    },
    lastLocation: {
        type: [Number], // [<longitude>, <latitude>]
        index: '2d',    // create the geospatial index
        default: [77.2166672, 28.6314512]
    },
    lastLoginDate: {
        type: Date,
        default: Date.now
    },

});

//Define the model for User
var User;
if(mongoose.models.User)
    User = mongoose.model('User');
else
    User = mongoose.model('User', UserSchema);

//Export the User Model
module.exports = User;
试试这个

 var PersonSchema = new Schema({
  firstName : String,
  lastName : String, 
    father : {
    firstName: String, 
    lastName: String  
    validate : validateFunction
  }, 
    mother : {
    firstName: String,
    lastName: String
     }
  };
试试这个

 var PersonSchema = new Schema({
  firstName : String,
  lastName : String, 
    father : {
    firstName: String, 
    lastName: String  
    validate : validateFunction
  }, 
    mother : {
    firstName: String,
    lastName: String
     }
  };

mongoose文档实际上建议使用嵌套模式,因此我们可以对对象进行验证

var ParentSchema = new Schema({
    firstName: String,
    lastName: String
 });
var PersonSchema = new Schema({
  firstName : String,
  lastName : String, 
  father : {
    type: ParentSchema, 
    validate : validateFunction
  }, 
  mother : {
    type: ParentSchema, 
    validate : validateFunction
  }
};
这将完成技巧和验证。

mongoose文档实际上建议使用嵌套模式,因此我们可以对对象进行验证

var ParentSchema = new Schema({
    firstName: String,
    lastName: String
 });
var PersonSchema = new Schema({
  firstName : String,
  lastName : String, 
  father : {
    type: ParentSchema, 
    validate : validateFunction
  }, 
  mother : {
    type: ParentSchema, 
    validate : validateFunction
  }
};

这应该可以完成技巧和验证。

谢谢,如果我没有正确陈述我的问题,很抱歉,我知道我可以在叶节点上添加验证,我想在叶节点的父节点上添加验证,即示例代码中的“地址”对象。谢谢,如果我没有正确陈述我的问题,很抱歉,我知道我可以在叶节点上添加验证,我想在叶节点的父节点上添加验证,即示例代码中的“address”对象。谢谢@Azem Malik,我已经用你的建议更新了我的问题,测试过了,似乎不需要将验证放在那里。谢谢@Azem Malik,我已经用你的建议更新了我的问题,只是把验证放在那里似乎不起作用。