These are chat archives for LearnBoost/mongoose

19th
Jan 2015
Pratik Bothra
@pratik60
Jan 19 2015 20:49
Hello guys :-)
Wondering if its possible to populate on an array of a reference field?
var TeamSchema = new Schema({
  name: {
    type: String,
    default: '',
    trim: true
  },
  user: {
    type: Schema.ObjectId,
    ref: 'User'
  },
  players: [{
    type: Schema.ObjectId,
    ref: 'Player'
  }],
  createdAt: {
    type: Date,
    default: Date.now
  },
  status: {
    type: Number
  }
});
TeamSchema.path('players').validate(function(players) {//Need to check that players size is equal to 7, and that the sum of players.value is < 100},'invalid team');
@chetverikov @suprememoocow - Any idea if the above is popular, how will i populate players in validation itself?
Andrew Newdigate
@suprememoocow
Jan 19 2015 20:56
Hmm. I haven't done much with Mongoose validation myself :(
Pratik Bothra
@pratik60
Jan 19 2015 20:57
Hmmm
But any idea what one should do?
Pratik Bothra
@pratik60
Jan 19 2015 21:05
Would you be doing this sort of validation inside an express route itself?
Valeri Karpov
@vkarpov15
Jan 19 2015 22:27
You can definitely add a check that players.length is equal to 7 in validation, but validating on a populated field is a bit trickier
You would first have to check if players is populated before doing that check
and its also no real guarantee of correctness because you can increment a players value without going through the TeamSchema at all
The check for players.length definitely makes sense to put in validation, but I'd do the player sum check in an API route