You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have raised this issue before on discord, and i think some things were modified but i still think there are problems with using
findByIdAndUpdate for a put request.
even when you use {runValidators: true}
it still only runs validation on the paths we supply to it.
a put request should replace a document with the data provided.
however if we dont provide a value for a required path in the update, it wont run the required validator on the path that has no value. Only the paths provided will be replaced and the path that wasnt supplied will still have its previous value.
The Issue
I have raised this issue before on discord, and i think some things were modified but i still think there are problems with using
findByIdAndUpdate for a put request.
even when you use {runValidators: true}
it still only runs validation on the paths we supply to it.
a put request should replace a document with the data provided.
however if we dont provide a value for a required path in the update, it wont run the required validator on the path that has no value. Only the paths provided will be replaced and the path that wasnt supplied will still have its previous value.
as pointed out in the mongoose docs
https://mongoosejs.com/docs/validation.html#update-validators
it also says in the mongoose docs
"The save() function is generally the right way to update a document with Mongoose. With save(), you get full validation and middleware."
https://mongoosejs.com/docs/documents.html#updating-using-queries
An example
if i have the model
where title is required
and my controller is as such
and my blog looks like so before the update
and i send the following data through a put request, with no title.
afterwards my data will be like so
and jrr hartley didnt write lord of the rings.
The text was updated successfully, but these errors were encountered: