提问者:小点点

验证基于枚举值的JSON子模式定义


我试图验证并报告构建过程中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匹配的子架构进行验证?


共1个答案

匿名用户

您可以使用if然后关键字如下。

{
  "$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"]
          }
        }
      }
    }
  }
}