JSON 数组选项的架构 oneOf

JSON Schema oneOf for array options

我正在尝试对 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"]
      ]
    }
  }
}     

失败 "Unexpected token when reading schemas: StartArray. Path '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/ 进行实验。

尝试使用枚举关键字。

The enum keyword is used to restrict a value to a fixed set of values. It must be an array with at least one element, where each element is unique.

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"]}
      ]
    }
  }
}  

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