假设以下 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"
]
}
]
}
要处理 types 属性中字符串数组的验证并确保至少存在一个必需的类型(locality、country、postal_code),您可以使用 JSON 架构来检查 types 数组是否至少包含其中一个值。以下是您的架构的改进版本:
{ "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "", "title": "Google API 地理编码", "description": "必要的地址组件。", "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": "array", "items": { "type": "string" } } } }, "allOf": [ { "contains": { "properties": { "types": { "type": "array", "contains": { "enum": ["locality"] } } } } }, { "contains": { "properties": { "types": { "type": "array", "contains": { "enum": ["country"] } } } } }, { "contains": { "properties": { "types": { "type": "array", "contains": { "enum": ["postal_code"] } } } } } ] } } } 要点: types 作为数组: types 属性现在正确定义为字符串数组。 contains 关键字:用于确保数组至少包含一个来自指定集合(locality、country、postal_code)的元素。 allOf 用于多项检查:该架构确保 address_components 数组至少包含一个具有地点的对象、一个具有国家/地区的对象和一个具有邮政编码的对象作为类型数组的一部分。此架构将正确验证 address_components 是否包含所需的地址类型。
我想这就是你所要求的。
每个
address_components > types
条目应至少包含三种类型关键字之一locality, postal_code, country
。