3

我有一个猫鼬模式(摘录如下):

var PersonSchema = new Schema({
  name : { 
      first: { type: String, required: true } 
    , last: { type: String, required: true }   
  } 
  ...

我想检查架构以确定哪些字段是必需的,然后验证这些字段是否存在于用户输入中。我可以测试 name.first 的“必需”属性,如下所示:

var person_schema = require('../models/person');

if (person_schema.schema.paths['name.first'].isRequired) {
  req.assert('first', messages.form_messages.msg_required).notEmpty();

但是感觉这是不安全的,因为内部架构细节可能会改变。有没有更好的办法?

4

1 回答 1

1

Mongoose 会为您进行此类验证。http://mongoosejs.com/docs/validation.html

于 2012-10-17T13:50:44.583 回答