New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unique validator incorrecttly flags reference object id in another collection as not unique #131
Comments
Thanks for the detailed report, I'll look into it. Possibly the actual cause of #88 |
Can you reduce the schemas to a minimal copy, and add the creation logic? I want to reduce this to a single snippet of code that caused the issue, checking out repos is less helpful. I have attempted to do this myself but so far have not seen the problem.
|
Thanks! It might be worth noting that the database is on atlas. I'll try to strip everything down as much as I can and post it. |
I have ran across this issue too, seems to be incorrect conditions when saving a non-new document. |
We already have logic in place to avoid validating self, something beyond that is happening. Hopefully #88 can provide insight. |
I just got the same issue here too |
v2.0.4 should be deprecated and unpublished from npm if possible, this causes major auth issues for probably thousands of projects out there |
there should have been a major semver bump for these changes |
2.0.4 was deprecated. unpublishing isn't available. 3.0 was pushed instead. |
Just email support@npmjs.com and tell them the severity of this and they should be able to force unpublish it from there side since it's only been a few days |
Appreciate the advice, I have emailed them. |
@Podesta I'm doing the same course, came here after encountering the same issue. You may very well have figured-out a workaround in the interim, but if not you can satisfy the (course specific) requirements while keeping mongoose v^6.0.7 by adding this to the express errorHandler middleware:
Use a try/catch block in your notes POST route, and make sure catching and passing on the exception with next(exception). 'unplug' mongoose-unique-validator from the User Schema, you shouldn't need the dependency anymore. Update the Jest tests to reflect the different status of the error . |
Same issue, will try to remove the plugin, since it's reporting errors on _id as well where there is no violation. The somewhat inconsistent occurrence leads me to believe that the plugin is actually checking the database with a find and using that is an indication of uniqueness which can cause all kinds of issues and false negatives/positives. No? Seems that correct save error can be simply handled through mongo errors and they contain the violating field as well. { index: 0,
code: 11000,
keyPattern: { username: 1 },
keyValue: { username: '1635256492062abcDEF' }
} |
Edit: It turns out the "fix" was having only one user when I recreated the DB. Upon creating another user entry it gave me the same error on update requests, as before.
Hope this is helpful, cheers! |
First of all, I can confirm that this bug is present on
"mongoose": "^6.0.7"
with"mongoose-unique-validator": "^3.0.0"
, but not present on"mongoose": "^5.11.13"
with"mongoose-unique-validator": "^2.0.3"
.As the title says, when marking, for example, the 'username' key as unique in a 'user' Schema. And then when I try to add a reference of this 'user', using his object '_id' in a different schema, for example a 'notes' Schema, it returns the following error, where the value printed is the '_id' that I want to store as reference.
Curiously enough, it only happens when there is more than one user in the collection. If I have a single user, I can keep adding new notes, without any issues, and the notes correctly store the user _id as a reference, and the users also correctly store a reference with the object _id from the notes.
Here are the two schemas, for reproducing the bug. Bug found while following Fullstackopen course. It might also be easier to reproduce by simply cloning the fullstack repo, and checking out to branch
part4-8
, and making sure updatepackage.json
to use the newest versions ofmongoose
andmongoose-unique-validator
, as stated on the first line.The text was updated successfully, but these errors were encountered: