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.
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 required
validator checks for both undefined
and null
.
I find it very counterintuitive to have to specify null
in an enum鈥檚 values to allow null
s while you could specify required: true
to forbid them.
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. :)
@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.
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
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 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?
@adamreisnz or set enum
to [null].concat(Object.values(genders))
.
@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.
Most helpful comment
@adamreisnz or set
enum
to[null].concat(Object.values(genders))
.