Check if array has an odd amount of items using JSON Schema

31 views Asked by At

Is it possible to check if an array of strings has an odd amount of items using JSON Schema?

This would be valid:

{
   list: [
      'one', 
      'two', 
      'three'
  ] 
} 

This would be valid as well:

{
   list: [
      'one', 
      'two', 
      'three', 
      'four', 
      'five' 
  ] 
} 

This would be invalid:

{
   list: [
      'one', 
      'two', 
      'three', 
      'four' 
  ] 
} 
1

There are 1 answers

1
Jason Desrosiers On BEST ANSWER

Unfortunately, this is one of those things you can't do with JSON Schema. If you have an upper limit on how many items can be in the array, you can fake it by checking every possible number items.

"anyOf": [
  { "minItems": 1, "maxItems": 1 },
  { "minItems": 3, "maxItems": 3 },
  { "minItems": 5, "maxItems": 5 },
  { "minItems": 7, "maxItems": 7 },
  { "minItems": 9, "maxItems": 9 },
  { "minItems": 11, "maxItems": 11 },
  { "minItems": 13, "maxItems": 13 },
  { "minItems": 15, "maxItems": 15 },
  { "minItems": 17, "maxItems": 17 },
  ... And so on up to the limit of items in the array ...
]