Skip to content

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

Closed

Description

@xileftenurb

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

Activity

  1. added this to the 5.7.4 milestone on Oct 7, 2019
  2. added
    has repro scriptThere is a repro script, the Mongoose devs need to confirm that it reproduces the issue
    on Oct 7, 2019
  3. added a commit that references this issue on Oct 7, 2019
  4. added
    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.
    and removed
    has repro scriptThere is a repro script, the Mongoose devs need to confirm that it reproduces the issue
    on Oct 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions