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

minLength ignored for items of array type? #136

Closed
AJB99 opened this issue Jan 20, 2017 · 3 comments
Closed

minLength ignored for items of array type? #136

AJB99 opened this issue Jan 20, 2017 · 3 comments

Comments

@AJB99
Copy link

AJB99 commented Jan 20, 2017

Hi there, another excellent mafintosh lib. I use mongojs on the daily. You've got a knack for UX.

I've run into an issue (bug?) when evaluating the minLength of any given array being validated. Consider the following:

var validate = validator({
    type: 'object',
    required: true,
    additionalProperties: false,
    properties: {
        pages : {
            type : 'array',
            required : true,
            minLength : 3,
            items : {
                type : 'object',
                additionalProperties : false,
                properties : {
                    title : {
                        type : 'string',
                        required : true
                    },
                    url : {
                        type : 'string',
                        required : true
                    }
                }
            }
        }
    }
}, {
    verbose : true
})

var data = {
    pages : [
        { title : 1, url : 'http://someurlhere.com' },
        { title : 2, url : 'http://someotherurlhere.com' }
    ]
}

var valid = validate(data); // true
if (validate.errors) {
    console.log(validate.errors)
}

I would expect this to throw since I've declared minLength: 3 for the pages array. But it passes even though I'm only sending two items through on the data object.

Is this the expected behaviour?

Additionally, when passing an empty array, the data validates just fine when I'm expecting an empty array to throw. Is this also the expected behaviour?

var data = {
    pages : []
}

var valid = validate(data); // true

Thanks for any light you can shed on this!

@LinusU
Copy link
Collaborator

LinusU commented Jan 20, 2017

This indeed seems like a bug, smaller test case:

https://runkit.com/5817cf4da433260014a11fe3/5882877ada465a00141b97a6/branches/master

var validator = require('is-my-json-valid')

var validate = validator({
  type: 'array',
  items: { type: 'string' },
  minLength: 3
})

console.log(`this should be false: ${validate(['a', 'b'])}`)

@AJB99
Copy link
Author

AJB99 commented Jan 21, 2017

I actually found the issue. It's not a bug, I just got the keyword wrong. It should be minItems instead of minLength:

https://runkit.com/5882e034402ada0014424d44/5882e034402ada0014424d45

var validator = require('is-my-json-valid')

var validate = validator({
  type: 'array',
  items: { type: 'string' },
  minItems: 3
})

console.log(`this should be false: ${validate(['a', 'b'])}`)

@AJB99 AJB99 closed this as completed Jan 21, 2017
@LinusU
Copy link
Collaborator

LinusU commented Jan 21, 2017

Cool 👍

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

No branches or pull requests

2 participants