1

我有一个这样的 JSON 对象。

{
  "test": bla bla bla
}

test可以是 0 到 120 之间的数字或空字符串。我想使用这样的 JSON 模式来验证这个 JSON 对象。

{
  "type": ["number", "string"],
  "enum": [""],
  "minimum": 0,
  "maximum": 120
}

有效的

{"test": ""}
{"test": 0}
{"test": 120}
{"test": 3}

无效的

{"test": "dfd"}
{"test": -1}
{"test": 675}

什么是正确的 JSON 模式?请帮忙

4

1 回答 1

3

试试这个架构

{
  "anyOf":[
    {
        "type": "string",
        "enum": [""]
    },
    {
        "type": "number",
        "minimum": 0,
        "maximum": 120
    }
  ]
}

希望这会有所帮助

于 2021-08-11T06:39:43.510 回答