-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Description
Do you want to request a feature or report a bug?
I think this a bug.
If it is by design, I would be very curious as to why, as it seem misleading to me.
What is the current behavior?
if I create a schema like this
mongoose.Schema({name : {type : String, required : undefined}});
then the "name" field is required
also, if I set it like so
mongoose.Schema({name : {type : String, required : null}})
then the schema Throw with the error "Cannot read property 'message' of null
What is the expected behavior?
I would expect both a field marked with "required : undefined" or "required : null" to simply not be required, as their are falsy value and by default, if "required" is not set, it is "false".
this cause a subtle bug where if I have different biaviour depending if I have an object without the property "required" or if I have it but with value "undefined".
At most, I would expect that if we don't want for null and undefined value to be concidered as "falsy", we could throw a meaningfull message.
For null, the error is there because at schematype.js:791
, there is the line if(typeof required === 'object')
, letting pass the "null" value as it was an object.
For undefined, the error is because at schematype.js:797
, there is the line if(required === false)
, not permitting undefined nor null.
do note, I don't know what to do if the value send is a string or number. Common sens would be probably to let normal javascript rules for coercion (so changing line 797 for if(!!required === false)
, and let a non-empty string or non-zero number be true, and an empty string or 0 be false.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
I use mongoose 5.7.2 and Node 11.7.0