JSON 架构未验证必需的属性
JSON Schema not validating required attributes
这里是标准:单个区域对象只能与数组共存 asia/europe/austrlia(从 asia/europe/austrlia 开始一次一个区域)。
除此之外,每个区域对象都可以有很少的必需属性和嵌套属性。
问题是模式验证器没有抱怨必需的属性(即维度对象的宽度属性)
这是JSON架构
{
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"stat_data": {
"type": "array",
"if": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [
"europe"
]
}
}
}
},
"then": {
"not": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [
"asia",
"australia"
]
}
}
}
}
},
"else": {
"if": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [
"asia"
]
}
}
}
},
"then": {
"not": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [
"europe",
"australia"
]
}
}
}
}
},
"else": {
"if": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [
"australia"
]
}
}
}
},
"then": {
"not": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [
"europe",
"asia"
]
}
}
}
}
},
"else": {}
}
},
"items": {
"type": "object",
"properties": {
"details": {
"$ref": "#/definitions/dimension"
},
"population": {
"$ref": "#/definitions/details"
},
"dimension": {
"$ref": "#/definitions/population"
},
"region": {
"enum": [
"asia",
"europe",
"australia",
"some-pencil-region",
"some-oil-pastels-region"
]
}
}
}
}
},
"definitions": {
"dimension": {
"type": "object",
"required": [
"width"
],
"properties": {
"height": {
"type": "integer"
},
"width": {
"type": "integer"
}
}
},
"details": {
"type": "object",
"properties": {
"brand": {
"type": "string"
},
"year": {
"type": "integer"
}
}
},
"population": {
"type": "object",
"properties": {
"change": {
"type": "integer"
},
"year": {
"type": "integer"
}
}
}
}
}
和JSON
{
"stat_data": [
{
"region": "some-pencil-region",
"details": {
"brand": "Camlin",
"year": 2019
}
},
{
"region": "some-oil-pastels-region",
"height": 30
},
{
"region": "asia",
"population": {
"year": 2018,
"change": 2
}
}
]
}
您可以在 _https://jsonschema.dev/ 通过复制架构和 json 在编辑器中查看
也许您的 JSON 验证器没有发现问题? JSONBuddy 显示有关丢失 "width" 属性:
的消息
这里是标准:单个区域对象只能与数组共存 asia/europe/austrlia(从 asia/europe/austrlia 开始一次一个区域)。
除此之外,每个区域对象都可以有很少的必需属性和嵌套属性。
问题是模式验证器没有抱怨必需的属性(即维度对象的宽度属性)
这是JSON架构
{
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"stat_data": {
"type": "array",
"if": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [
"europe"
]
}
}
}
},
"then": {
"not": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [
"asia",
"australia"
]
}
}
}
}
},
"else": {
"if": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [
"asia"
]
}
}
}
},
"then": {
"not": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [
"europe",
"australia"
]
}
}
}
}
},
"else": {
"if": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [
"australia"
]
}
}
}
},
"then": {
"not": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [
"europe",
"asia"
]
}
}
}
}
},
"else": {}
}
},
"items": {
"type": "object",
"properties": {
"details": {
"$ref": "#/definitions/dimension"
},
"population": {
"$ref": "#/definitions/details"
},
"dimension": {
"$ref": "#/definitions/population"
},
"region": {
"enum": [
"asia",
"europe",
"australia",
"some-pencil-region",
"some-oil-pastels-region"
]
}
}
}
}
},
"definitions": {
"dimension": {
"type": "object",
"required": [
"width"
],
"properties": {
"height": {
"type": "integer"
},
"width": {
"type": "integer"
}
}
},
"details": {
"type": "object",
"properties": {
"brand": {
"type": "string"
},
"year": {
"type": "integer"
}
}
},
"population": {
"type": "object",
"properties": {
"change": {
"type": "integer"
},
"year": {
"type": "integer"
}
}
}
}
}
和JSON
{
"stat_data": [
{
"region": "some-pencil-region",
"details": {
"brand": "Camlin",
"year": 2019
}
},
{
"region": "some-oil-pastels-region",
"height": 30
},
{
"region": "asia",
"population": {
"year": 2018,
"change": 2
}
}
]
}
您可以在 _https://jsonschema.dev/ 通过复制架构和 json 在编辑器中查看
也许您的 JSON 验证器没有发现问题? JSONBuddy 显示有关丢失 "width" 属性:
的消息