【问题标题】:Json property structure dependant of another propertyJson 属性结构依赖于另一个属性
【发布时间】:2018-05-09 13:19:20
【问题描述】:

我一直在使用 json 架构来验证来自我的一个网络服务的答案。

答案分为两个属性:datastatus。如果 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"
    }
}

目前这部分代码通过了,不知道为什么。

【问题讨论】:

    标签: jsonschema


    【解决方案1】:

    这里有一些问题,所以我会尝试解释所有这些。你是在正确的轨道上!

    "properties": {
        "code": 0
    }
    

    “属性”的值必须是一个对象。此对象的每个值 必须是有效的 JSON 架构。

    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": {
    

    [definitions] 关键字的值必须是一个对象。 this的每个成员值
    对象必须是有效的 JSON 架构。

    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 松弛服务器!也很高兴在这里发表评论。

    【讨论】:

    • 哦,好的,我明白我错过了关于属性值的内容。到目前为止,我还没有在任何地方看到“additionalProperties”键,它会非常有用!
    • 不客气!如果您接受答案,您也可以投票赞成。
    猜你喜欢
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 2021-04-02
    相关资源
    最近更新 更多