question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PUT (update) does not run mongoose validators

See original GitHub issue

Hi,

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:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sean-nicholascommented, Oct 9, 2016

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

When using update validators, required validators only fail when you try to explicitly $unset the key.

0reactions
dafflcommented, Aug 14, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found