繁体   English   中英

数组选项的JSON模式oneOf

[英]JSON Schema oneOf for array options

我正在尝试对JSON有效负载具有“步骤”数组且其内容可能只是预定义的一组选项之一进行建模的场景,例如:

{ "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]'””

编辑

为移动球门柱而道歉,我将问题简化到了这样的程度,即所提出的解决方案仅适用于简化版本,而不适用于原始版本。 额外的麻烦是,我需要$ref对象代替string值,所以...

输入样例:

{ "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/进行实验。

尝试使用enum关键字。

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

json-schema-更多信息

在您的情况下,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"]}
      ]
    }
  }
}  

根据您的修改更新了我的答案。 以下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"
       }
    ]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM