Проверка определения подсхемы JSON на основе значения ENUM - PullRequest
0 голосов
/ 14 июня 2019

Я пытаюсь проверить и сообщить об ошибках схемы 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 Ответ

0 голосов
/ 15 июня 2019

Вы можете использовать ключевые слова if и then следующим образом.

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