【问题标题】:JSON Schema not validating required attributesJSON Schema 未验证必需的属性
【发布时间】:2020-02-05 01:38:09
【问题描述】:

这是标准:单个区域对象可以与阵列共存,并且只能与 asia/europe/austrlia 一起(一次来自 asia/europe/austrlia 的一个区域)。

除此之外,每个区域对象都可以有很少的必需属性和嵌套属性。

问题是模式验证器没有抱怨所需的属性(即维度对象的宽度属性)

这是 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 来查看

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    也许您的 JSON 验证器没有发现问题? JSONBuddy 显示有关缺少“宽度”属性的消息:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多