JSONSchema:Google GeoCoding API - 包含强制对象的地址组件,其数组至少具有一个特定值

问题描述 投票:0回答:1

假设来自 Google GeoCoding API 的以下 JSON

{
    "lon": 1.2345678,
    "lat": 1.2345678,
    "formatted_adress": "Sunny Ave, 01234 Dev City, New Devland",
    "address_components": [
        {
            "long_name": "Sunny Ave",
            "short_name": "Sunny Ave"
            "types": [
                "route"
            ]
        },
        {
            "long_name": "Downtown",
            "short_name": "Downtown",
            "types": [
                "sublocality_level_1",
                "sublocality",
                "political"
            ]
        },
        {
            "long_name": "Dev City",
            "short_name": "Dev City",
            "types": [
                "locality",
                "political"
            ]
        },
        {
            "long_name": "Dev City",
            "short_name": "Dev City",
            "types": [
                "administrative_area_level_3",
                "political"
            ]
        },
        {
            "long_name": "Somwherechester",
            "short_name": "SC",
            "types": [
                "administrative_area_level_1",
                "political"
            ]
        },
        {
            "long_name": "New Devland",
            "short_name": "DE",
            "types": [
                "country",
                "political"
            ]
        },
        {
            "long_name": "01234",
            "short_name": "01234",
            "types": [
                "postal_code"
            ]
        }
    ]
}

我需要验证所示的数据(类型、属性等)。

除了:

address_components
必须包含
types
至少包含 [
locality
,
country
,
postal_code
] 中的一个元素的所有对象。允许添加其他物体。

基于此解决方案https://github.com/orgs/json-schema-org/discussions/191如果

types
是一个常量字符串,我设法足够接近。但我没能成功验证字符串数组。

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "",
    "title": "Google APIs Geo Coding",
    "description": "Necessary address components.",
    "type": "object",
    "required": [
        "formatted_adress",
        "lon",
        "lat",
        "address_components"
    ],
    "properties": {
        "formatted_adress": {
            "type": "string"
        },
        "lon": {
            "type": "number"
        },
        "lat": {
            "type": "number"
        },
        "address_components": {
            "type": "array",
            "items": {
                "type": "object",
                "required": [
                    "long_name",
                    "short_name",
                    "types"
                ],
                "properties": {
                    "long_name": {
                        "type": "string"
                    },
                    "short_name": {
                        "type": "string"
                    },
                    "types": {
                        "type": "string"
                    }
                }
            },
            "allOf": [
                {
                    "contains": {
                        "properties": {
                            "types": {
                                "const": "locality"
                            }
                        }
                    }
                },
                {
                    "contains": {
                        "properties": {
                            "types": {
                                "const": "country"
                            }
                        }
                    }
                },
                {
                    "contains": {
                        "properties": {
                            "types": {
                                "const": "postal_code"
                            }
                        }
                    }
                }
            ]
        }
    }
}

示例

无效数据 => 没有带有

postal_code
的对象。

{
    "lon": 1.2345678,
    "lat": 1.2345678,
    "formatted_adress": "Sunny Ave, 01234 Dev City, New Devland",
    "address_components": [
        {
            "long_name": "Sunny Ave",
            "short_name": "Sunny Ave"
            "types": [
                "route"
            ]
        },
        {
            "long_name": "Downtown",
            "short_name": "Downtown",
            "types": [
                "sublocality_level_1",
                "sublocality",
                "political"
            ]
        },
        {
            "long_name": "Dev City",
            "short_name": "Dev City",
            "types": [
                "locality",
                "political"
            ]
        },
        {
            "long_name": "Dev City",
            "short_name": "Dev City",
            "types": [
                "administrative_area_level_3",
                "political"
            ]
        },
        {
            "long_name": "Somwherechester",
            "short_name": "SC",
            "types": [
                "administrative_area_level_1",
                "political"
            ]
        },
        {
            "long_name": "New Devland",
            "short_name": "DE",
            "types": [
                "country",
                "political"
            ]
        }
    ]
}

有效数据 => 所有 3 个对象至少具有上述类型之一

{
    "lon": 1.2345678,
    "lat": 1.2345678,
    "formatted_adress": "Sunny Ave, 01234 Dev City, New Devland",
    "address_components": [
        {
            "long_name": "Dev City",
            "short_name": "Dev City",
            "types": [
                "locality",
                "political"
            ]
        },
        {
            "long_name": "New Devland",
            "short_name": "DE",
            "types": [
                "country",
                "political"
            ]
        },
        {
            "long_name": "01234",
            "short_name": "01234",
            "types": [
                "postal_code"
            ]
        }
    ]
}
jsonschema
1个回答
0
投票

我想这就是你所要求的。

每个

address_components > types
条目应至少包含三种类型关键字
locality, postal_code, country
之一。

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "",
  "title": "Google APIs Geo Coding",
  "description": "Necessary address components.",
  "type": "object",
  "required": [
    "formatted_address",
    "lon",
    "lat",
    "address_components"
  ],
  "properties": {
    "formatted_address": {"type": "string"},
    "lon": {"type": "number"},
    "lat": {"type": "number"},
    "address_components": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "long_name",
          "short_name",
          "types"
        ],
        "properties": {
          "long_name": {"type": "string"},
          "short_name": {"type": "string"},
          "types": {
            "type": "array",
            "items": {
              "enum": [
                "administrative_area_level_1",
                "administrative_area_level_3",
                "country",
                "locality",
                "political",
                "postal_code",
                "route",
                "sublocality",
                "sublocality_level_1"
              ]
            },
            "contains": {
              "anyOf": [
                {"const": "country"},
                {"const": "locality"},
                {"const": "postal_code"}
              ]
            }
          }
        }
      }
    }
  }
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.