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

support schemas for empty arrays #980

Closed
mfulton26 opened this issue Sep 3, 2020 · 9 comments
Closed

support schemas for empty arrays #980

mfulton26 opened this issue Sep 3, 2020 · 9 comments

Comments

@mfulton26
Copy link

I need to validate that an array is present and empty under certain scenarios. I tried "items": [] but that produces and "invalid" schema: data.items should NOT have fewer than 1 items (AJV errorsText)

i.e. I'd like to do the following:

{ "type": "array", "items": [] }

or maybe:

{ "type": "array", "items": false }

But I had to do this instead (maybe there is a better way?):

{ "type": "array", "maxItems": 0 }
@Relequestual
Copy link
Member

What's wrong with maxItems?

@notEthan
Copy link
Contributor

notEthan commented Sep 3, 2020

"items": false does what you want. you could also do "const": [] or maxItems.

@mfulton26
Copy link
Author

hmmm, const seems nice

I suppose nothing is really wrong with maxItems: 0 it just wasn't what I thought of first and I was surprised when I was informed that items: [] wasn't valid

@mfulton26
Copy link
Author

mfulton26 commented Sep 3, 2020

[1, 2, 3] validates for me against items: false using ajv which was unexpected

@Relequestual
Copy link
Member

That's strange... Looks like items: false against [1,2,3] works for me.
https://jsonschema.dev/s/SEUZM

The instance would be valid if the array was empty, because the subschema value of items is applied to each value in the applicable array.

@Relequestual
Copy link
Member

const may also be a good option, as without it, you'll need to define type in order to make the value IS an array in the first place, as you have done.

@mfulton26
Copy link
Author

this might be a bug in Ajv

ajv-validator/ajv#1270

thanks

@Relequestual
Copy link
Member

OK. Weird. My site uses a version of ajv, but probably not the latest... =/

@mfulton26
Copy link
Author

@Relequestual it only seems to be an issue when Ajv is configured with strictKeywords: true

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

3 participants