Json 属性 结构依赖于另一个 属性
Json property structure dependant of another property
我一直在研究 json 模式来验证来自我的一个网络服务的答案。
答案分为两个属性:data
和 status
。如果 status.code
设置为 0,则 data
将必须遵守特定架构。否则,如果 status.code
设置为 -1,则不会读取 data
,因此我不想检查它是否符合架构。
这是架构:
{
"$schema": "http://json-schema.org/schema#",
"id": "file://registration.json",
"type": "object",
"properties": {
"status": {
"$ref": "#/definitions/classes/status"
}
},
"anyOf": [
{
"$ref": "#/definitions/conditions/status-is-ok"
},
{
"$ref": "#/definitions/conditions/status-is-nok"
}
],
"definitions": {
"classes": {
"status": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"message": {
"type": "string"
}
},
"required": [
"code",
"message"
]
},
"data": {
"type": "object",
"properties": {
"propertyA": {
"type": "#/definitions/classes/metadatauser"
},
"propertyB": {
"type": "#/definitions/classes/membreinfo"
}
},
"required": ["propertyA", "propertyB"]
}
},
"conditions": {
"status-is-ok": {
"status": {
"properties": {
"code": 0
}
},
"data": {
"$ref": "#/definitions/classes/data"
}
},
"status-is-nok": {
"status": {
"properties": {
"code": -1
}
},
"data": {
"type": "object"
}
}
}
}
}
下面是不应验证的示例:
{
"data": {},
"status": {
"code": 0,
"message": "OK"
}
}
目前,这部分代码通过了,我不知道为什么。
你在这里有一些错误,所以我会尽力解释所有这些。你走对了!
"properties": {
"code": 0
}
The value of "properties" MUST be an object. Each value of this object
MUST be a valid JSON Schema.
http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.5.4
您不能将您期望的值作为 属性 键的值。
但是,您可以使用 [const]
1 关键字来实现特定值验证。
"$ref": "#/definitions/conditions/status-is-ok"
...
"conditions": {
"status-is-ok": {
"status": {
"properties": {
[The definitions
] keyword's value MUST be an object. Each member value of this
object MUST be a valid JSON Schema.
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-9
这意味着您需要将定义中每个键的每个值视为一个 JSON 架构。如果您有一个 JSON 架构,其中您没有将“状态”嵌套在 properties
对象中,则不会进行任何验证。 “数据”也是如此。
(严格来说,根据规范的定义部分,您不得在定义对象中深入嵌套模式,但这似乎得到了一些实现的支持,并使用正确的解析规则进行解析。前缀可能是更好。)
完整的固定架构如下
{
"$schema": "http://json-schema.org/schema#",
"id": "file://registration.json",
"type": "object",
"properties": {
"status": {
"$ref": "#/definitions/classes/status"
}
},
"anyOf": [
{
"$ref": "#/definitions/conditions/status-is-ok"
},
{
"$ref": "#/definitions/conditions/status-is-nok"
}
],
"definitions": {
"classes": {
"status": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"message": {
"type": "string"
}
},
"required": [
"code",
"message"
]
},
"data": {
"type": "object",
"properties": {
},
"required": [
"propertyA",
"propertyB"
]
}
},
"conditions": {
"status-is-ok": {
"properties": {
"status": {
"properties": {
"code": {
"const": 0
}
}
},
"data": {
"$ref": "#/definitions/classes/data"
},
},
"additionalProperties": false
},
"status-is-nok": {
"properties": {
"status": {
"properties": {
"code": {
"const": -1
}
}
},
"data": {
"type": "object"
},
},
"additionalProperties": false
}
}
}
}
如果其中有任何不合理之处,请务必告诉我。
如果您想进一步讨论任何方面,请随时加入 JSON Schema slack 服务器!也很高兴在这里发表评论。
我一直在研究 json 模式来验证来自我的一个网络服务的答案。
答案分为两个属性:data
和 status
。如果 status.code
设置为 0,则 data
将必须遵守特定架构。否则,如果 status.code
设置为 -1,则不会读取 data
,因此我不想检查它是否符合架构。
这是架构:
{
"$schema": "http://json-schema.org/schema#",
"id": "file://registration.json",
"type": "object",
"properties": {
"status": {
"$ref": "#/definitions/classes/status"
}
},
"anyOf": [
{
"$ref": "#/definitions/conditions/status-is-ok"
},
{
"$ref": "#/definitions/conditions/status-is-nok"
}
],
"definitions": {
"classes": {
"status": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"message": {
"type": "string"
}
},
"required": [
"code",
"message"
]
},
"data": {
"type": "object",
"properties": {
"propertyA": {
"type": "#/definitions/classes/metadatauser"
},
"propertyB": {
"type": "#/definitions/classes/membreinfo"
}
},
"required": ["propertyA", "propertyB"]
}
},
"conditions": {
"status-is-ok": {
"status": {
"properties": {
"code": 0
}
},
"data": {
"$ref": "#/definitions/classes/data"
}
},
"status-is-nok": {
"status": {
"properties": {
"code": -1
}
},
"data": {
"type": "object"
}
}
}
}
}
下面是不应验证的示例:
{
"data": {},
"status": {
"code": 0,
"message": "OK"
}
}
目前,这部分代码通过了,我不知道为什么。
你在这里有一些错误,所以我会尽力解释所有这些。你走对了!
"properties": {
"code": 0
}
The value of "properties" MUST be an object. Each value of this object MUST be a valid JSON Schema.
http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.5.4
您不能将您期望的值作为 属性 键的值。
但是,您可以使用 [const]
1 关键字来实现特定值验证。
"$ref": "#/definitions/conditions/status-is-ok"
...
"conditions": {
"status-is-ok": {
"status": {
"properties": {
[The
definitions
] keyword's value MUST be an object. Each member value of this
object MUST be a valid JSON Schema.
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-9
这意味着您需要将定义中每个键的每个值视为一个 JSON 架构。如果您有一个 JSON 架构,其中您没有将“状态”嵌套在 properties
对象中,则不会进行任何验证。 “数据”也是如此。
(严格来说,根据规范的定义部分,您不得在定义对象中深入嵌套模式,但这似乎得到了一些实现的支持,并使用正确的解析规则进行解析。前缀可能是更好。)
完整的固定架构如下
{
"$schema": "http://json-schema.org/schema#",
"id": "file://registration.json",
"type": "object",
"properties": {
"status": {
"$ref": "#/definitions/classes/status"
}
},
"anyOf": [
{
"$ref": "#/definitions/conditions/status-is-ok"
},
{
"$ref": "#/definitions/conditions/status-is-nok"
}
],
"definitions": {
"classes": {
"status": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"message": {
"type": "string"
}
},
"required": [
"code",
"message"
]
},
"data": {
"type": "object",
"properties": {
},
"required": [
"propertyA",
"propertyB"
]
}
},
"conditions": {
"status-is-ok": {
"properties": {
"status": {
"properties": {
"code": {
"const": 0
}
}
},
"data": {
"$ref": "#/definitions/classes/data"
},
},
"additionalProperties": false
},
"status-is-nok": {
"properties": {
"status": {
"properties": {
"code": {
"const": -1
}
}
},
"data": {
"type": "object"
},
},
"additionalProperties": false
}
}
}
}
如果其中有任何不合理之处,请务必告诉我。 如果您想进一步讨论任何方面,请随时加入 JSON Schema slack 服务器!也很高兴在这里发表评论。