用于验证一个参数的多种类型和值的 json 架构

Ale*_*ats 5 validation schema json

请帮我解决这个问题:我尝试编写一个 json 模式来验证以下对象:

json 对象:

{"param":value}
Run Code Online (Sandbox Code Playgroud)

可能的值:'all',[任何整数的数组]

所以它是一个简单的 json 对象,其中包含一个变量,可以是字符串“all”,也可以是任何整数数组 []。

我尝试了这个,但它在 json 模式验证器中不起作用:

 { 
     "type": ["string","array"], 
     "items": { "oneOf":  [  
      "all", 
      { "type": "integer" } 
        ] 
     }
}
Run Code Online (Sandbox Code Playgroud)

谢谢。

小智 5

对于 Draft4,这个模式应该可以工作

{
  "type": "object",
  "properties": {
    "param": {
      "oneOf": [
        {
          "enum": ["all"]
        },
        {
          "type": "array",
          "items": {"type": "integer"}
        }
      ]
    }
  },
  "additionalProperties": false,
  "required": ["param"]
}
Run Code Online (Sandbox Code Playgroud)

的值oneOf应该是对象列表,关键字enum允许与值进行比较。