使用 JSON Schema 检查数组是否有奇数个项目

问题描述 投票:0回答:1

是否可以使用 JSON Schema 检查字符串数组是否包含奇数个项目?

这将是有效的:

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

这也是有效的:

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

这将是无效的:

{
   list: [
      'one', 
      'two', 
      'three', 
      'four' 
  ] 
} 
json jsonschema json-schema-validator ajv
1个回答
0
投票

不幸的是,这是 JSON Schema 无法做到的事情之一。如果您对数组中可以包含的项目数量有上限,则可以通过检查每个可能的项目数量来伪造它。

"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 ...
]
© www.soinside.com 2019 - 2024. All rights reserved.