JSON 架构:从列表中选择单个值应有效

JSON schema: Selecting a single value from a list should be valid

提问人:klausk 提问时间:6/28/2023 更新时间:6/28/2023 访问量:21

问:

为了验证输入字段,我想创建一个 JSON 架构。 这是关于时区的选择。所有可能的时区都存储在架构中。我的问题是,如果多个时区位于一个大陆下,则单个时区的选择是无效的。

缩短的 SCHEMA:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Continents Timezones",
  "type": "object",
  "properties": {
    "continents": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "continent": {
            "type": "string"
          },
          "timezones": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "timezone": {
                  "type": "string"
                },
                "utc_offset": {
                  "type": "string"
                },
                "dst_offset": {
                  "type": "string"
                }
              },
              "required": [
                "timezone",
                "utc_offset",
                "dst_offset"
              ]
            }
          }
        },
        "required": [
          "continent",
          "timezones"
        ],
        "enum": [
          {
            "continent": "Africa",
            "timezones": [
              {
                "timezone": "Africa/Abidjan",
                "utc_offset": "+00:00",
                "dst_offset": "+00:00"
              },
              {
                "timezone": "Africa/Accra",
                "utc_offset": "+00:00",
                "dst_offset": "+00:00"
              },
              {
                "timezone": "Africa/Addis_Ababa",
                "utc_offset": "+03:00",
                "dst_offset": "+03:00"
              },
              {
                "timezone": "Africa/Algiers",
                "utc_offset": "+01:00",
                "dst_offset": "+01:00"
              },
              {
                "timezone": "Africa/Asmara",
                "utc_offset": "+03:00",
                "dst_offset": "+03:00"
              },
              {
                "timezone": "Africa/Bamako",
                "utc_offset": "+00:00",
                "dst_offset": "+00:00"
              }
            ]
          },
          {
            "continent": "UTC",
            "timezones": [
              {
                "timezone": "UTC",
                "utc_offset": "+00:00",
                "dst_offset": "+00:00"
              }
            ]
          }
        ]
      }
    }
  },
  "required": [
    "continents"
  ]
}

以下 JSON 应有效:

{
  "continents": [
    {
            "continent": "Africa",
            "timezones": [
              {
                "timezone": "Africa/Abidjan",
                "utc_offset": "+00:00",
                "dst_offset": "+00:00"
              }
      ]
    }
  ]
}

如果大陆下只有一个时区(如 UTC),则 JSON 有效。

JSON 验证 架构

评论


答: 暂无答案