当嵌套文档存在时,如何验证嵌套文档的属性是否存在?

2023-12-29

用户架构.js

var Schema = require('mongoose').Schema;
var uniqueValidator = require('mongoose-unique-validator');
var _ = require('lodash');

var userSchema = new Schema({
  local: {
    username: String, // should exist when local exists
    role: String,
    hashedPassword: { type: String, select: false }
  },

  facebook: {
    id: String,
    token: { type: String, select: false }
  },

  twitter: {
    id: String,
    token: { type: String, select: false }
  },

  google: {
    id: String,
    token: { type: String, select: false }
  }
});

userSchema.path('local').validate(function(local) {
  var empty = _.isEmpty(local);
  if (empty) {
    return true;
  }
  else if (!empty && local.username) {
    return true;
  }
  else if (!empty && !local.username) {
    return false;
  }
}, 'Local auth requires a username.');

module.exports = userSchema;

我正在尝试验证这一点username存在时local不为空。 IE。当使用本地身份验证时,username应该存在。

// should validate
user = {
  local: {
    username: 'foo';
    hashedPassword: 'sfsdfs'
  }
};

// shouldn't validate
user = {
  local: {
    hashedPassword: 'sdfsdfs'
  }
};

// should validate (because local isn't being used)
user = {
  local: {},
  facebook {
    ...
  }
};

我收到此错误:

/Users/azerner/code/mean-starter/server/api/users/user.schema.js:51
userSchema.path('local').validate(function(local) {
                        ^
TypeError: Cannot read property 'validate' of undefined

看来你无法获取path的物体。我学会了here https://stackoverflow.com/questions/17035297/getting-schema-attributes-from-mongoose-model模式有一个paths财产。当我console.log(userSchema.paths):

{ 'local.username':
   { enumValues: [],
     regExp: null,
     path: 'local.username',
     instance: 'String',
     validators: [],
     setters: [],
     getters: [],
     options: { type: [Function: String] },
     _index: null },
  'local.role':
   { enumValues: [],
     regExp: null,
     path: 'local.role',
     instance: 'String',
     validators: [],
     setters: [],
     getters: [],
     options: { type: [Function: String] },
     _index: null },
  'local.hashedPassword':
   { enumValues: [],
     regExp: null,
     path: 'local.hashedPassword',
     instance: 'String',
     validators: [],
     setters: [],
     getters: [],
     options: { type: [Function: String], select: false },
     _index: null,
     selected: false },
  'facebook.id':
   { enumValues: [],
     regExp: null,
     path: 'facebook.id',
     instance: 'String',
     validators: [],
     setters: [],
     getters: [],
     options: { type: [Function: String] },
     _index: null },
  'facebook.token':
   { enumValues: [],
     regExp: null,
     path: 'facebook.token',
     instance: 'String',
     validators: [],
     setters: [],
     getters: [],
     options: { type: [Function: String], select: false },
     _index: null,
     selected: false },
  'twitter.id':
   { enumValues: [],
     regExp: null,
     path: 'twitter.id',
     instance: 'String',
     validators: [],
     setters: [],
     getters: [],
     options: { type: [Function: String] },
     _index: null },
  'twitter.token':
   { enumValues: [],
     regExp: null,
     path: 'twitter.token',
     instance: 'String',
     validators: [],
     setters: [],
     getters: [],
     options: { type: [Function: String], select: false },
     _index: null,
     selected: false },
  'google.id':
   { enumValues: [],
     regExp: null,
     path: 'google.id',
     instance: 'String',
     validators: [],
     setters: [],
     getters: [],
     options: { type: [Function: String] },
     _index: null },
  'google.token':
   { enumValues: [],
     regExp: null,
     path: 'google.token',
     instance: 'String',
     validators: [],
     setters: [],
     getters: [],
     options: { type: [Function: String], select: false },
     _index: null,
     selected: false },
  _id:
   { path: '_id',
     instance: 'ObjectID',
     validators: [],
     setters: [ [Function: resetId] ],
     getters: [],
     options: { type: [Object], auto: true },
     _index: null,
     defaultValue: [Function: defaultId] } }

所以看起来路径就像local.username and facebook.token存在,但不存在像这样的“顶级”路径local and facebook.

如果我尝试验证local.username路径,它并不像我想要的那样工作。

userSchema.path('local.username').validate(function(username) {
  return !!username
}, 'Local auth requires a username.');

仅当以下情况时才应用验证local.username存在。我想要验证它存在。因此,当它不存在时,不会应用验证,因此它被认为是有效的并被保存。

我也尝试了以下方法,但结果是一样的local.username方法(当用户名不存在时,验证不会被命中,并且它被标记为有效)。

var Schema = require('mongoose').Schema;
var uniqueValidator = require('mongoose-unique-validator');
var _ = require('lodash');

var userSchema = new Schema({
  local: {
    username: {
      type: String,
      validate: [validateUsernameRequired, 'Local auth requires a username.']
    },
    role: String,
    hashedPassword: { type: String, select: false }
  },

  facebook: {
    id: String,
    token: { type: String, select: false }
  },

  twitter: {
    id: String,
    token: { type: String, select: false }
  },

  google: {
    id: String,
    token: { type: String, select: false }
  }
});

function validateUsernameRequired(username) {
  return !!username;
}

module.exports = userSchema;

Adam,为什么不尝试一个预验证挂钩,有条件地将错误传递给下一个函数。我认为这将为您提供所需的灵活性。如果不起作用请告诉我。

例如

schema.pre('validate', function(next) {
  if(/*your error case */){ next('validation error text') }
  else { next() }
})

这将导致猫鼬发送一个ValidationError返回到尝试保存文档的人。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当嵌套文档存在时,如何验证嵌套文档的属性是否存在? 的相关文章

随机推荐