Схема JSON не проверяет обязательные атрибуты - PullRequest
0 голосов
/ 07 октября 2019

Вот критерии: отдельный объект региона может сосуществовать с массивом вместе с ТОЛЬКО Азией / Европой / Австралией (один регион за один раз из Азии / Европы / Австрии).

В дополнениек этому каждый объект региона может иметь несколько обязательных и вложенных атрибутов.

Проблема заключается в том, что средство проверки схемы не жалуется на требуемый атрибут (т. е. атрибут width из объекта измерения)

ВотСхема JSON

{
  "type": "object",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "properties": {
    "stat_data": {
      "type": "array",
      "if": {
        "contains": {
          "type": "object",
          "properties": {
            "region": {
              "type": "string",
              "enum": [
                "europe"
              ]
            }
          }
        }
      },
      "then": {
        "not": {
          "contains": {
            "type": "object",
            "properties": {
              "region": {
                "type": "string",
                "enum": [
                  "asia",
                  "australia"
                ]
              }
            }
          }
        }
      },
      "else": {
        "if": {
          "contains": {
            "type": "object",
            "properties": {
              "region": {
                "type": "string",
                "enum": [
                  "asia"
                ]
              }
            }
          }
        },
        "then": {
          "not": {
            "contains": {
              "type": "object",
              "properties": {
                "region": {
                  "type": "string",
                  "enum": [
                    "europe",
                    "australia"
                  ]
                }
              }
            }
          }
        },
        "else": {
          "if": {
            "contains": {
              "type": "object",
              "properties": {
                "region": {
                  "type": "string",
                  "enum": [
                    "australia"
                  ]
                }
              }
            }
          },
          "then": {
            "not": {
              "contains": {
                "type": "object",
                "properties": {
                  "region": {
                    "type": "string",
                    "enum": [
                      "europe",
                      "asia"
                    ]
                  }
                }
              }
            }
          },
          "else": {}
        }
      },
      "items": {
        "type": "object",
        "properties": {
          "details": {
            "$ref": "#/definitions/dimension"
          },
          "population": {
            "$ref": "#/definitions/details"
          },
          "dimension": {
            "$ref": "#/definitions/population"
          },
          "region": {
            "enum": [
              "asia",
              "europe",
              "australia",
              "some-pencil-region",
              "some-oil-pastels-region"
            ]
          }
        }
      }
    }
  },
  "definitions": {
    "dimension": {
      "type": "object",
      "required": [
        "width"
      ],
      "properties": {
        "height": {
          "type": "integer"
        },
        "width": {
          "type": "integer"
        }
      }
    },
    "details": {
      "type": "object",
      "properties": {
        "brand": {
          "type": "string"
        },
        "year": {
          "type": "integer"
        }
      }
    },
    "population": {
      "type": "object",
      "properties": {
        "change": {
          "type": "integer"
        },
        "year": {
          "type": "integer"
        }
      }
    }
  }
}

и JSON

{
  "stat_data": [
    {
      "region": "some-pencil-region",
      "details": {
        "brand": "Camlin",
        "year": 2019
      }
    },
    {
      "region": "some-oil-pastels-region",
      "height": 30
    },
    {
      "region": "asia",
      "population": {
        "year": 2018,
        "change": 2
      }
    }
  ]
}

Вы можете проверить на _ https://jsonschema.dev/, скопировав схему и json в редактор

1 Ответ

1 голос
/ 11 октября 2019

Может быть, ваш валидатор JSON не уловил проблему? JSONBuddy показывает сообщение об отсутствующем свойстве width:

enter image description here

...