Skip to content
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

Closed
windkomo opened this issue Jun 2, 2015 · 11 comments · Fixed by #13620
Closed

null in enum #3044

windkomo opened this issue Jun 2, 2015 · 11 comments · Fixed by #13620
Assignees
Labels
discussion If you have any thoughts or comments on this issue, please share them!
Milestone

Comments

@windkomo
Copy link

windkomo commented Jun 2, 2015

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.

@vkarpov15
Copy link
Collaborator

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".

@tusbar
Copy link
Contributor

tusbar commented Jun 2, 2015

@vkarpov15 though the required validator checks for both undefined and null.

I find it very counterintuitive to have to specify null in an enum’s values to allow nulls while you could specify required: true to forbid them.

@vkarpov15
Copy link
Collaborator

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.

@tusbar
Copy link
Contributor

tusbar commented Jun 2, 2015

Fair enough. :)

@qwelias
Copy link

qwelias commented Mar 7, 2017

@vkarpov15

undefined value -- primitive value used when a variable has not been assigned a value
null value -- primitive value that represents the intentional absence of any object value

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.

whereas enum generally means either the value was never defined or the value is one of the allowed values

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.

@sobafuchs
Copy link
Contributor

sobafuchs commented Mar 7, 2017

i think the point is that null is a value (ok sort of, just bear with me here) while undefined isn't. Specifically, the value that null holds is the absence of any value. You're intentionally setting something to what you refer to as a no-value with null

@qwelias
Copy link

qwelias commented Mar 7, 2017

They both are primitive values.

@adamreisnz
Copy link
Contributor

adamreisnz commented Dec 7, 2017

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 null in that list. However, if an organisation defines their default gender, they must be able to select "no default" (e.g. null). It is not possible for me to specify an enum and also allow null as a valid value, which is a bit of a hassle.

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?

@tusbar
Copy link
Contributor

tusbar commented Dec 7, 2017

@adamreisnz or set enum to [null].concat(Object.values(genders)).

@adamreisnz
Copy link
Contributor

@tusbar thanks, yes I prefer Object.values(genders).concat([null]) because it reads better. I guess it's compact enough to do it like that so no point really in having the config param.

@vkarpov15 vkarpov15 reopened this Jun 8, 2023
@vkarpov15 vkarpov15 added this to the 8.0 milestone Jun 8, 2023
@vkarpov15 vkarpov15 added the discussion If you have any thoughts or comments on this issue, please share them! label Jun 8, 2023
@vkarpov15
Copy link
Collaborator

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 null and undefined interchangeably. It is a little strange that null is fine for non-required paths unless enum is specified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion If you have any thoughts or comments on this issue, please share them!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants