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

Required array field throws error when set as empty array #2662

Closed
Soviut opened this issue Feb 10, 2015 · 3 comments
Closed

Required array field throws error when set as empty array #2662

Soviut opened this issue Feb 10, 2015 · 3 comments

Comments

@Soviut
Copy link

Soviut commented Feb 10, 2015

I have an array field with the following schema on that field:

parents:
    type: 'Array'
    required: true

When I try to create a document with the following data:

data =
    parents: []

Translation.create data, (err, translations) ->
    console.log err, translations

I get the following error:

ValidationError: Path `parents` is required.

If I put a value inside the array it works, like so:

data =
    parents: ['blah']

Similarly, if I remove the required: true from the schema, it also starts working.

Is an empty array not considered satisfactory for "required" validation? I understand it's false in javascript, but there are plenty of cases where empty arrays and objects are intended in Mongo documents.

@paton
Copy link
Contributor

paton commented Apr 13, 2016

Agreed, we got tripped up on this too.

Assumed behavior of required: true for us was the presence of an array, rather than an array that requires at least one element.

@almassapargali
Copy link

Agree that required array fields should accept empty arrays. I've always thought of it this way, till I had an issue with Mongoose yesterday.

@vkarpov15 vkarpov15 added this to the 5.0 milestone Jun 8, 2016
@vkarpov15 vkarpov15 removed this from the Parking Lot milestone Dec 6, 2017
@vkarpov15
Copy link
Collaborator

Gonna close this in favor of #5139. Will change this behavior in 5.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants