1

I have a basic Mongoose model with a Meeting and Participants array:

var MeetingSchema = new Schema({
    description: {
        type: String
    },
    maxNumberOfParticipants: {
        type: Number
    },
    participants: [ {
        type: Schema.ObjectId,
        ref: 'User'
    } ]
});

Let's say I want to validate that the number of participants added doesn't exceed the maxNumberOfParticipants for that meeting.

I've thought through a few options:

  1. Custom Validator - which I can't do because I have to validate one attribute (participants length) against another (maxNumberOfParticipants).
  2. Middleware - i.e., pre-save. I can't do this either because my addition of participants occurs via a findOneAndUpdate (and these don't get called unless I use save).
  3. Add validation as part of my addParticipants method. This seems reasonable, but I'm not sure how to pass back a validation error from the model.

Note that I don't want to implement the validation in the controller (express, MEAN.js stack) because I'd like to keep all logic and validations on the model.

Here is my addParticipants method:

MeetingSchema.methods.addParticipant = function addParticipant(params, callback) {
  var Meeting = mongoose.model('Meeting');
  if (this.participants.length == this.maxNumberOfParticipants) {
      // since we already have the max length then don't add one more
      return ????
  }
  return Meeting.findOneAndUpdate({ _id: this.id },
      { $addToSet: { participants: params.id } },
      {new: true})
      .populate('participants', 'displayName')
      .exec(callback);
};

Not sure how to return a validation error in this case or even if this pattern is a recommended approach.

Arthur Frankel
  • 4,695
  • 6
  • 35
  • 56
  • With the help of my good friend digger69 I was after to pass back an error as such: callback({errors: [{message: 'Too many participants'}]}); This works in the context of the mean.js (boilerplate) we are using and seems like a good answer, but I'm curious if this is the right approach still. – Arthur Frankel Jul 21 '14 at 16:20

1 Answers1

0

I wouldn't think that's it's common practice for this to be done at the mongoose schema level. Typically you will have something in between the function getting called and the database layer (your schema) that performs some kind of validation (such as checking max count). You would want your database layer to be in charge of just doing simple/basic data manipulation that way you don't have to worry about any extra dependencies when/if anything else calls it. This may mean you'd need to go with route 1 that you suggested, yes you would need to perform a database request to find out what your current number of participants but I think it the long run it will help you :)

jmadewell
  • 170
  • 1
  • 5
  • Thank you for your response. I think you are suggesting my #1 a custom validator, but that would be at the schema level, right? Also, in order to do that I would need an extra database call (as you mention) and that doesn't seem right if I already have all of the data while executing my function. – Arthur Frankel Jul 21 '14 at 11:55