假设以下 JSON 来自 Google GeoCoding API
{
"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"
]
}
]
}