数组选项的JSON模式oneOf

时间:2019-08-07 11:27:02

标签: json jsonschema

我正在尝试模拟JSON有效负载具有“ steps”数组且其内容可能只是预定义的一组选项之一的情况,例如:

{ "steps": ["s0"] } 
or
{ "steps": ["s1"] }
or
{ "steps": ["s0", "s2"] }

如何在模式中对此建模?以下:

{
  "$id": "https://example.com/person.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",

  "title": "Payload",
  "type": "object",

  "properties": {
    "steps": {
      "type": "array",
      "oneOf": [
        ["s0"],
        ["s1"],
        ["s0", "s2"]
      ]
    }
  }
}     

失败,并出现“读取架构时出现意外令牌:StartArray。路径'properties.steps.oneOf [0]'”

编辑

道歉,我简化了我的问题,使提出的解决方案可以简化 版本,但不是原始版本。额外的麻烦是,我需要string个对象而不是$ref个对象……

样本输入:

{ "steps": [{"name": "S0"}] } 
or
{ "steps": [{"name": "S1"}] }
or
{ "steps": [{"name": "S1"}, {"name": "S2"}] }

与预期不匹配的架构(遵循@EylM的建议)

{
  "$id": "https://example.com/person.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Request",
  "type": "object",
  "properties": {
    "steps": {
      "type": "array",
      "oneOf": [
        {
          "const": [
            {"$ref": "#/definitions/s0"}
          ]
        },
        {
          "const": [
            {"$ref": "#/definitions/s1"}
          ]
        },
        {
          "const": [
            {"$ref": "#/definitions/s1"},
            {"$ref": "#/definitions/s2"}
          ]
        }
      ]
    }
  },
  "required": [
    "steps"
  ],
  "definitions": {
    "s0": {
      "type": "object",
      "properties": {"name": { "const": "S0" }}
    },
    "s1": {
      "type": "object",
      "properties": {"name": {"const": "S1" }}
    },
    "s2": {
      "type": "object",
      "properties": {"name": { "const": "S2" }}
      }
    }
}

使用此架构并输入{ "steps":[{"name": "s0"}] },我得到 JSON在'oneOf'中没有任何模式时有效

无论价值多少,我都在使用https://www.jsonschemavalidator.net/进行实验。

2 个答案:

答案 0 :(得分:1)

尝试使用enum关键字。

  

enum关键字用于将一个值限制为一组固定的值。   它必须是一个至少包含一个元素的数组,其中每个元素都是   

json-schema - More info

在您的情况下,JSON类似于:

{
  "$id": "https://example.com/person.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",

  "title": "Payload",
  "type": "object",

  "properties": {
    "steps": {
      "type": "array",
      "oneOf": [
        {"enum": ["s1"]},
        {"enum": ["s0"]},
        {"enum": ["s1, s2"]}
      ]
    }
  }
}  

答案 1 :(得分:1)

根据您的修改更新了我的答案。以下JSON模式会验证问题中的所有3个条件,并且我假设{ "steps": [{"name": "S2"}, {"name": "S1"}] }无效。如果我错了请纠正我。

{
  "$id": "https://example.com/person.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Request",
  "type": "object",
  "properties": {
    "steps": {
      "type": "array",
      "anyOf": [
        {
          "maxItems": 1,
          "items": {
            "oneOf": [
              {
                "$ref": "#/definitions/s0"
              },
              {
                "$ref": "#/definitions/s1"
              }
            ]
          }
        },
        {
          "minItems": 2,
          "maxItems": 2,
          "items": [
            {
              "$ref": "#/definitions/s1"
            },
            {
              "$ref": "#/definitions/s2"
            }
          ]
        }
      ]
    }
  },
  "required": [
    "steps"
  ],
  "definitions": {
    "s0": {
      "type": "object",
      "properties": {
        "name": {
          "const": "S0"
        }
      }
    },
    "s1": {
      "type": "object",
      "properties": {
        "name": {
          "const": "S1"
        }
      }
    },
    "s2": {
      "type": "object",
      "properties": {
        "name": {
          "const": "S2"
        }
      }
    }
  }
}

如果要通过{ "steps": [{"name": "S2"}, {"name": "S1"}] }的验证,请使用另一个anyOf块,如下所示。

{
    "minItems": 2,
    "maxItems": 2,
    "items": [
       {
          "$ref": "#/definitions/s2"
       },
       {
          "$ref": "#/definitions/s1"
       }
    ]
}