Consistent pre-save validation for findByIdAndUpdate
<p>I am trying to find a consistent way to validate some of the ids I am saving into an array.</p>

<p>Schema:</p>

<pre><code>var MySchema = new Schema({
  name: { type: String },
  otherIds: [{type: Schema.Types.ObjectId, ref: 'Other'}]
});
</code></pre>

<p>A couple of update statements:</p>

<p>Update entire doc based on id</p>

<pre><code>var update = {
  otherIds: someIds
};
MyModel.findByIdAndUpdate(id, event, ...);
</code></pre>

<p>Lets just add to the otherIds set:</p>

<pre><code>var update = {
  $addToSet: {
    otherIds: someId
  }
};
MyModel.findByIdAndUpdate(id, update, ...);
</code></pre>

<p>So as far as I know there are a few ways to validate those ids, but I have run into problems with all of them.</p>

<ol>
<li><p>Use save middleware.  This does not work as save middleware is not called when using findAndUpdate commands.</p></li>
<li><p>Use findOneAndUpdate middleware.  This is probably the most promising as this middleware is going to be called in both cases.  However trying to extract the otherIds from the update statement is inconsistent, as each update statement is very different.  This seems fragile as anytime someone adds a new update statement they would potentially have to modify this middleware to accommodate a possibly different update statement.</p></li>
<li><p>Use validator.  I have tried using a validator and it works great for the first update, but is not run for the second update.  According to the docs, validators are only run for $set and $unset commands, and $addToSet does not fall into that category.</p></li>
</ol>

<p>Are there any other options or anything I may have missed?</p>