-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
bug in mongoose.Types.ObjectId.isValid() #11209
Comments
For me it results in const mongoose = require('mongoose');
const whatIsThis = { id: '580e0797bb495f0a200e91ad' };
console.log(whatIsThis, 'mongoose.Types.ObjectId.isValid', mongoose.Types.ObjectId.isValid(whatIsThis)); 6.1.5:
6.1.6:
Note: |
@Maximusya thx for the help. re-checked and found that the isValid changed behaviour from [email protected] to [email protected]
So the issue is caused by bson npm module. This PR causes the change in the isValid behaviour: isValid now also accepts ObjectIdLike type, which matches an object like { id: '580e0797bb495f0a200e91ad'} Defined in file on line 13: |
This behaviour change seems to be intended by bson module maintainers. So is
now to be considered to be a valid ObjectId? To introduce such a breaking change in a bson patch version update is odd. |
Right now I am migrating from Mongoose 5.x to 6.x and got about half of integration tests fail just because of undocumented changes in ObjectId validation.
I was just about to start a discussion about the issue when I saw your bug report. |
@Maximusya I think I will add a test to our test suite which validates the isValid() behaviour to guard against such changes. |
@Maximusya I've written a cypress test to check isValid() behaviour:
|
I have actually gathered some thoughts here: #11221 |
Yeah i agree. But the issue is that the bson package is just some dumb patched code. Also it was always(?) "clear" that a jump from bson 1 to bson 4 will break a lot. Anyway... I agree with you that isValidObjectId and ObjectId.isValid should have the same result. I think the issue arises from Line 956 in a8a615c
What should happen is actually using the validation method from bson and not reinventing the wheel again. But to fix that maybe makes sense to use your test cases and integrate them into mongoose and check if the isValidObjectId is behaving the same as ObjectId.isValid. |
…make `ObjectId()` casting more flexible Re: #11209
@maximilianschmid can you please elaborate why this is a breaking change? The intent of In the meantime, as per @Uzlopak 's suggestion, we'll make |
bug in mongoosejs 6.1.6 version:
ObjectId.isValid validates an Object with prop id as a valid ObjectId:
mongoose.Types.ObjectId.isValid({ id: '580e0797bb495f0a200e91ad' }) // true
But in 6.1.5 this is an invalid ObjectId
mongoose.Types.ObjectId.isValid({ id: '580e0797bb495f0a200e91ad' }) // false
The text was updated successfully, but these errors were encountered: