mongoose version: 5.0.1
MongoDB version: 3.6.2
node.js version: 9.3.0
When saving, the enum validation works as expected but when updating using the findOneAndUpdate
method the enum validation does not work.
Almost exactly the same problem as described here, the only difference is that I'm using $addToSet
: https://github.com/Automattic/mongoose/issues/860#issuecomment-168012110
If this is not possible yet, which is the recommended way to validate enums on update?
I just found out that the only thing necessary to make it work is passing the runValidators: true
in the options of the findOneAndUpdate
method, like this:
const order = await Order.findOneAndUpdate({_id: req.params.id}, req.body, {new: true, runValidators: true});
Most helpful comment
I just found out that the only thing necessary to make it work is passing the
runValidators: true
in the options of thefindOneAndUpdate
method, like this:const order = await Order.findOneAndUpdate({_id: req.params.id}, req.body, {new: true, runValidators: true});