【问题标题】:Validation JSON subschema definition based on ENUM value基于 ENUM 值的验证 JSON 子模式定义
【发布时间】:2019-06-14 20:21:11
【问题描述】:

我正在尝试验证和报告构建过程的 JSON 架构错误。

基于type 枚举,我想针对特定的子模式进行验证并报告该模式的错误。

如果type 属性是“weblogic”,那么我只想验证“weblogic”子模式定义。如果类型是 tomcat 也一样

这是我当前的架构

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "#",
  "type": "object",
  "title": "The Root Schema",
  "required": [
    "middleware"
  ],
  "properties": {
    "middleware": {
      "$ref": "#/definitions/middleware"
    }
  },
  "definitions": {
    "middleware":{
      "type": "array",
      "items": {
        "oneOf":[
          {"$ref": "#/definitions/weblogic"},
          {"$ref": "#/definitions/tomcat"}
        ],
        "required": ["type","buildInfo"]
      }
    },
    "weblogic": {
      "properties": {
        "type": {"const": "weblogic"},
        "buildInfo": {
          "properties": {
            "adminSslPort": {
              "type": "integer"
            }
          },
          "additionalProperties": false,
          "required": ["adminSslPort"]
        }
      }
    },
    "tomcat":{
      "properties": {
        "type": {"const": "tomcat"},
        "buildInfo":{
          "properties": {
            "classpath": {
              "type": "string"
            }
          },
          "additionalProperties": false,
          "required": ["classpath"]
        }
      }
    }
  }
}

还有我的 JSON 负载

{
    "middleware":[
        {
            "type": "weblogic",
            "buildInfo":{
                "adminSslPort": 7002
            }
        },
        {
            "type": "tomcat",
            "buildInfo":{

            }
        }
    ]
}

现在我预计这会失败,因为 buildInfo 对象缺少 classpath 属性,并且确实无法通过验证。 但是我得到的错误包括针对“weblogic”定义的验证错误。

[
    {
        "pointerToViolation": "#/middleware/1",
        "causingExceptions": [
            {
                "schemaLocation": "#/definitions/weblogic",
                "pointerToViolation": "#/middleware/1",
                "causingExceptions": [
                    {
                        "schemaLocation": "#/definitions/weblogic/properties/buildInfo",
                        "pointerToViolation": "#/middleware/1/buildInfo",
                        "causingExceptions": [],
                        "keyword": "required",
                        "message": "required key [adminSslPort] not found"
                    },
                    {
                        "schemaLocation": "#/definitions/weblogic/properties/type",
                        "pointerToViolation": "#/middleware/1/type",
                        "causingExceptions": [],
                        "keyword": "const",
                        "message": ""
                    }
                ],
                "message": "2 schema violations found"
            },
            {
                "schemaLocation": "#/definitions/tomcat/properties/buildInfo",
                "pointerToViolation": "#/middleware/1/buildInfo",
                "causingExceptions": [],
                "keyword": "required",
                "message": "required key [classpath] not found"
            }
        ],
        "keyword": "oneOf",
        "message": "#: 0 subschemas matched instead of one"
    }
]

有没有办法只验证 type 匹配的子模式?

【问题讨论】:

    标签: json jsonschema


    【解决方案1】:

    您可以使用ifthen 关键字,如下所示。

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "$id": "#",
      "type": "object",
      "title": "The Root Schema",
      "required": [
        "middleware"
      ],
      "properties": {
        "middleware": {
          "$ref": "#/definitions/middleware"
        }
      },
      "definitions": {
        "middleware":{
          "type": "array",
          "items": {
            "type": "object",
            "allOf": [
              {"$ref": "#/definitions/weblogic"},
              {"$ref": "#/definitions/tomcat"}
            ],
            "required": ["type","buildInfo"]
          }
        },
        "weblogic": {
          "if": {
            "properties": {
              "type": {
                "const": "weblogic"
              }
            }
          },
          "then": {
            "properties": {
              "buildInfo": {
                "properties": {
                  "adminSslPort": {
                    "type": "integer"
                  }
                },
                "additionalProperties": false,
                "required": ["adminSslPort"]
              }
            }
          }
        },
        "tomcat":{
          "if": {
            "properties": {
              "type": {
                "const": "tomcat"
              }
            }
          },
          "then": {
            "properties": {
              "buildInfo":{
                "properties": {
                  "classpath": {
                    "type": "string"
                  }
                },
                "additionalProperties": false,
                "required": ["classpath"]
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 谢谢!我想这正是我想要的。
    猜你喜欢
    • 2019-09-14
    • 1970-01-01
    • 2017-12-04
    • 2016-08-06
    • 1970-01-01
    • 2021-06-16
    • 2014-10-16
    • 2020-07-30
    • 2017-04-26
    相关资源
    最近更新 更多