-
-
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
null in enum #3044
Comments
Because null is not the same thing as undefined :) The high level semantics are that undefined means a value hasn't been set, whereas null means the value has been explicitly set to a value that "represents the intentional absence of any object value". |
@vkarpov15 though the I find it very counterintuitive to have to specify |
I disagree. Absence of a value and value never defined makes sense for required IMO, whereas enum generally means either the value was never defined or the value is one of the allowed values. Arguing about what's more intuitive is typically not very constructive though - I'm down to change if its a real pain point for a lot of users, but the difference is a one-liner. |
Fair enough. :) |
They both are primitive values and they both tell that there's no actual value, the only difference is that null is intentional. That's why required does check against them.
So basically enum checks the value against a set of values, right? And why should it try to verify null if null is actually a no-value? Enum should not try to check either null or undefined because they are both, in the first place, are no-values. |
i think the point is that |
They both are primitive values. |
I find that I am also running into this. I have for example an enum list of valid genders. These are strings, and I don't want to include Is there a possibility to be able to configure this in the schema, e.g. something like: //Some constant
const genders = {
FEMALE: 'female',
MALE: 'male',
OTHER: 'other',
};
//Field schema
{
type: String,
enum: Object.values(genders),
allowNull: true,
} @vkarpov15 if it's as little work as a one liner as you mentioned, would this be something you'd be interested to include in Mongoose for added flexibility? |
@adamreisnz or set |
@tusbar thanks, yes I prefer |
Popped up again in #13469 . This issue has come up a few times recently, and is a bit of a weird edge case in Mongoose's general policy of treating |
Is there a reason why null is not treated the same as undefined in enums ?
Shouldn't setting required to false (or omitting the attribute) allow null value ?
I've seen in tests that null is added to enums.
The text was updated successfully, but these errors were encountered: