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

时间:2019-06-14 20:21:11

标签: json jsonschema

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

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

如果remove()属性是“ weblogic”,那么我只想针对“ weblogic”子模式定义进行验证。如果类型是tomcat,则执行相同的操作

这是我当前的模式

type

还有我的JSON有效负载

type

现在,我希望这样做会失败,因为{ "$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"] } } } } } 对象缺少{ "middleware":[ { "type": "weblogic", "buildInfo":{ "adminSslPort": 7002 } }, { "type": "tomcat", "buildInfo":{ } } ] } 属性,并且它的确通过了验证。 但是我得到的错误包括针对“ weblogic”定义的验证错误。

buildInfo

是否有一种方法只能针对classpath所匹配的子模式进行验证?

1 个答案:

答案 0 :(得分:0)

您可以按以下方式使用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"]
          }
        }
      }
    }
  }
}