How do I validate any item of any array against any possible values in my enum?

29 views
Skip to first unread message

me.a

unread,
Aug 18, 2017, 9:51:59 AM8/18/17
to JSON Schema
I am looking to validate the values of an array against a list. The array should be able to contain any combination of five values in any order, but any item that is not one of those five values should invalidate the array. Thus, it should also never have more than five items. 

Ex: possible values: ["a","b","c","d","e"]

Sample Valid arrays: ["a"], ["a","b"], ["e","c"],["a","d","c"] ... etc. 

Sample Invalid arrays: ["a","g"], ["a","b","c","d","e","g"]


I tried the following, which was valid, but it did not seem to work to validate a json file. Any help? Is this possible? 

"Variable": {
"type": "array",
"items": [
{
"type": "string",
"enum": ["a", "b", "c", "d", "e"]
},
{
"type": "string",
"enum": ["a", "b", "c", "d", "e"]
},
{
"type": "string",
"enum": ["a", "b", "c", "d", "e"]
},
{
"type": "string",
"enum": ["a", "b", "c", "d", "e"]
},
{
"type": "string",
"enum": ["a", "b", "c", "d", "e"]
}
],
"minItems": 0,
"maxItems": 5
},

Roy Walmsley

unread,
Aug 18, 2017, 1:23:06 PM8/18/17
to JSON Schema, tsh...@gmail.com
Hi,

I am interested in this issue. I have two questions I'd like to ask you, please.

1) Do you want all the array items to be unique? That is ["a", "a"] is invalid.

2) What do you mean by validation "did not seem to work"? Does it fail to give any results, or incorrectly validate, or what?

I've had a similar issue. So far I've only solved it by using "OneOf" and generating sufficient subschemas to cover all possible permutations. If there is a better way, I also want to know it.

Roy

Felix Meschberger

unread,
Aug 18, 2017, 3:37:59 PM8/18/17
to json-...@googlegroups.com, tsh...@gmail.com
Hi

Would uniqueItems work ?

Regards
Felix

Von meinem iPad gesendet
--
You received this message because you are subscribed to the Google Groups "JSON Schema" group.
To unsubscribe from this group and stop receiving emails from it, send an email to json-schema...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Henry Andrews

unread,
Aug 18, 2017, 4:10:07 PM8/18/17
to JSON Schema, tsh...@gmail.com
I would just use this if duplicates are OK:

{
   
"type": "array",
   
"items": {

       
"enum": ["a", "b", "c", "d", "e"]
   
},
   
"minItems": 0,
   
"maxItems": 5
}


and this if you want to disallow duplicates:

{
   
"type": "array",
   
"items": {

       
"enum": ["a", "b", "c", "d", "e"]
   
},
   
"minItems": 0,
   
"maxItems": 5,

   
"uniqueItems": true
}

Since you are using "minItems"/"maxItems" to control the array size, you do not need to separately specify each position in the array.  You can just use the single-schema form of "items".

thanks,
-henry
Reply all
Reply to author
Forward
0 new messages