PUT (update) does not run mongoose validators
See original GitHub issueHi,
I created a really simple model, called tag
, with only one property, text
, which is required.
text: {
type: String,
required: true,
unique: true,
}
When I POST a new tag
, text
is required, and I got an error if text
is not set.
However, if a do a PUT request on /tags/:idOfTheNewTag with an empty body, my tag is replaced, with a new one with no text
. So, that PUT request did not thow an error for the “required” validator.
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Why Mongoose doesn't validate on update? - Stack Overflow
Mongoose 4.0 introduces an option to run validators on update() and findOneAndUpdate() calls. Turning this option on will run validators for all ...
Read more >Problem Solving: Mongoose Validators Don't Run on Update ...
Trouble arose when we needed to update (or upsert) our collections. Left to its own devices, mongoose's native findOneAndUpdate won't run ...
Read more >Mongoose v6.8.2: Validation
To turn on update validators, set the runValidators option for update() , updateOne() , updateMany() , or findOneAndUpdate() . Be careful: update validators...
Read more >Mongoose findOneAndUpdate and runValidators not working
I am having issues trying to get the 'runValidators' option to work. My post schema has an comments.replies.creator field that has required ...
Read more >Thoughts on running validators when updating - Google Groups
Mongoose wisely adds a nice $set to the update queries. So, it's basically a matter of checking what you are about to set....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have the same issue. It does not validate on patch, too
EDIT: Ahh found the issue. From http://mongoosejs.com/docs/validation.html#update-validator-paths
Going to close this since it is pretty old and this seems to be a Mongoose restriction which can be circumenvented by the aforementioned validate hook or the validateSchema common hook.