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

fields with settings "required : undefined" are required #8219

Closed
xileftenurb opened this issue Oct 4, 2019 · 0 comments
Closed

fields with settings "required : undefined" are required #8219

xileftenurb opened this issue Oct 4, 2019 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@xileftenurb
Copy link

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

@vkarpov15 vkarpov15 added this to the 5.7.4 milestone Oct 7, 2019
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Oct 7, 2019
vkarpov15 added a commit that referenced this issue Oct 7, 2019
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Oct 7, 2019
This was referenced Oct 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants