-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Fix: in some Number casts there were an assert twice and was not handling undefined #8725
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
Conversation
…ling undefined. fixes: #8711
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on your stack trace, there is only 1 error, just that error has a reason
property that is itself an error.
Also, your PR doesn't do anything. Using double equals to check for null
is a common shorthand because v == null
is equivalent to v === null || v === undefined
.
@vkarpov15 notice that the PR also removes the isNaN check |
yes, because there is already the same assert: Line 29 in bfc10f5
why are there the same code twice? weird. |
weird, why mongoose not working with Number as undefined that's all I am saying. Super weird. COVID-19!!! :) |
Now it will be like this (the assert is there, but allow for working with undefined). 'use strict';
const assert = require('assert');
/*!
* Given a value, cast it to a number, or throw a `CastError` if the value
* cannot be casted. `null` and `undefined` are considered valid.
*
* @param {Any} value
* @param {String} [path] optional the path to set on the CastError
* @return {Boolean|null|undefined}
* @throws {Error} if `value` is not one of the allowed values
* @api private
*/
module.exports = function castNumber(val) {
if (val == null) {
return val;
}
if (val === '') {
return null;
}
if (typeof val === 'string' || typeof val === 'boolean') {
val = Number(val);
}
assert.ok(!isNaN(val));
if (val instanceof Number) {
return val.valueOf();
}
if (typeof val === 'number') {
return val;
}
if (!Array.isArray(val) && typeof val.valueOf === 'function') {
return Number(val.valueOf());
}
if (val.toString && !Array.isArray(val) && val.toString() == Number(val)) {
return Number(val);
}
assert.ok(false);
}; |
if you keep this dual assert you will like this code: how can i fix this error as it says, it to not throw an error with undefined Number Mongoose Model? |
Mongoose doc for Number cast says: Given a value, cast it to a number, or throw a `CastError` if the value
* cannot be casted. `null` and `undefined` are considered valid. But if this cast happening as |
Thanks for helping! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me 👍
This is a breaking change, we already have tests asserting that null, and undefined are not allowed for SchemaType of number. @vkarpov15 should I remove the now-failing tests that assert that?
|
We also shouldn't be allowing NaN to go through if we decide that we want to allow undefined. This may do the trick: assert.ok(!isNaN(val) || val === undefined); |
@AbdelrahmanHafez nope, apparently the fact that Mongoose allows I will remove the test that asserts |
@vkarpov15 Great. Perhaps we should be dropping the support for that quirk in the 6.0. |
fixes: #8711
Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. The two fields below are mandatory.
If you're making a change to documentation, do not modify a
.html
file directly. Instead find the corresponding.pug
file or test case in thetest/docs
directory.Summary
#8711
Examples