JSONSchema(草稿7)未验证数组的子定义

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSONSchema(草稿7)未验证数组的子定义相关的知识,希望对你有一定的参考价值。

我对为什么我的JSONSchema无法按预期验证我的数据感到非常困惑。

我在下面附加了我的JSONShema和示例日期。

该模式毫无疑问地验证了嵌套数据结构(组织)的第一层,但未能验证第二层(用户)及以下。

有人可以指出我做错了正确的方向吗?

非常感谢!

JSONSchema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://myorg/json/schemas/report-schedule.json",
  "type": "array",
  "items": {
    "$ref": "#/definitions/Organisation"
  },
  "definitions": {
    "Organisation": {
      "type": "object",
      "required": [
        "organisationId",
        "organisationName",
        "users"
      ],
      "properties": {
        "organisationId": {
          "type": "string",
          "minLength": 1
        },
        "organisationName": {
          "type": "string",
          "minLength": 1
        },
        "users": {
          "type": "array",
          "items": {
            "ref": "#/definitions/User"
          }
        }
      }
    },
    "User": {
      "type": "object",
      "required": [
        "name",
        "email",
        "reports"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "email": {
          "type": "string",
          "format": "email"
        },
        "reports": {
          "type": "array",
          "items": {
            "ref": "#/definitions/Report"
          }
        }
      }
    },
    "Report": {
      "type": "object",
      "required": [
        "reportType",
        "reportWeekEndDay",
        "sendDay",
        "sendHour",
        "locations",
        "widgets"
      ],
      "properties": {
        "reportType": {
          "type": "string",
          "enum": [
            "org-weekly"
          ]
        },
        "reportWeekEndDay": {
          "type": "integer",
          "maximum": 6,
          "minimum": 0
        },
        "sendDay": {
          "type": "integer",
          "maximum": 6,
          "minimum": 0
        },
        "sendHour": {
          "type": "integer",
          "maximum": 23,
          "minimum": 0
        },
        "locations": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "widgets": {
          "$ref": "#/definitions/Widgets"
        }
      }
    },
    "Widgets": {
      "type": "array",
      "title": "Widgets within a report",
      "items": {
        "$ref": "#/definitions/Widget"
      }
    },
    "Widget": {
      "type": "object",
      "required": [
        "widgetType",
        "metrics",
        "comparisonType"
      ],
      "properties": {
        "widgetType": {
          "type": "string",
          "enum": [
            "table-individual-locations",
            "table-all-locations"
          ]
        },
        "comparisonType": {
          "type": "string",
          "enum": [
            "week-on-week",
            "3-and-1-month-weekly-avg"
          ]
        },
        "metrics": {
          "$ref": "#/definitions/Metrics"
        }
      }
    },
    "Metrics": {
      "type": "array",
      "title": "The Available Metrics",
      "items": {
        "oneOf": [
          {
            "$ref": "#/definitions/FootFallAndMovement"
          },
          {
            "$ref": "#/definitions/Engagement"
          },
          {
            "$ref": "#/definitions/Occupancy"
          },
          {
            "$ref": "#/definitions/SalesDataTransactions"
          },
          {
            "$ref": "#/definitions/SalesDataConversion"
          }
        ]
      }
    },
    "FootFallAndMovement": {
      "type": "object",
      "required": [
        "title",
        "type",
        "endpoint",
        "queryParams"
      ],
      "properties": {
        "title": {
          "type": "string",
          "minLength": 1
        },
        "type": {
          "type": "string",
          "const": "FootFallAndMovement"
        },
        "endpoint": {
          "type": "string",
          "minLength": 1
        },
        "queryParams": {
          "type": "object",
          "required": [
            "excludeStaff"
          ],
          "properties": {
            "excludeStaff": {
              "type": "boolean"
            }
          }
        }
      }
    },
    "Engagement": {
      "type": "object",
      "required": [
        "title",
        "type",
        "endpoint",
        "queryParams"
      ],
      "properties": {
        "title": {
          "type": "string",
          "minLength": 1
        },
        "type": {
          "type": "string",
          "const": "Engagement"
        },
        "endpoint": {
          "type": "string",
          "minLength": 1
        },
        "queryParams": {
          "type": "object",
          "required": [
            "excludeStaff",
            "contexts"
          ],
          "properties": {
            "additionalFilters": {
              "type": "object",
              "required": [
                "ignoreLingerUnder"
              ],
              "properties": {
                "ignoreLingerUnder": {
                  "type": "integer",
                  "minimum": 1
                }
              }
            },
            "excludeStaff": {
              "type": "boolean"
            },
            "contexts": {
              "type": "object",
              "required": [
                "type",
                "value"
              ],
              "properties": {
                "type": {
                  "type": "string",
                  "const": "taxonomy"
                },
                "value": {
                  "type": "string",
                  "minLength": 1
                }
              }
            }
          }
        }
      }
    },
    "Occupancy": {
      "type": "object",
      "required": [
        "title",
        "type",
        "endpoint",
        "queryParams"
      ],
      "properties": {
        "title": {
          "type": "string",
          "minLength": 1
        },
        "type": {
          "type": "string",
          "const": "Occupancy"
        },
        "endpoint": {
          "type": "string",
          "minLength": 1
        },
        "queryParams": {
          "type": "object",
          "required": [
            "excludeStaff",
            "contexts"
          ],
          "properties": {
            "excludeStaff": {
              "type": "boolean"
            },
            "contexts": {
              "type": "object",
              "required": [
                "type",
                "value"
              ],
              "properties": {
                "type": {
                  "type": "string",
                  "const": "taxonomy"
                },
                "value": {
                  "type": "string",
                  "minLength": 1
                }
              }
            }
          }
        }
      }
    },
    "SalesDataTransactions": {
      "type": "object",
      "required": [
        "title",
        "type",
        "endpoint"
      ],
      "properties": {
        "title": {
          "type": "string",
          "minLength": 1
        },
        "type": {
          "type": "string",
          "const": "SalesDataTransactions"
        },
        "endpoint": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "SalesDataConversion": {
      "type": "object",
      "required": [
        "title",
        "type",
        "endpoint",
        "queryParams"
      ],
      "properties": {
        "title": {
          "type": "string",
          "minLength": 1
        },
        "type": {
          "type": "string",
          "const": "SalesDataConversion"
        },
        "endpoint": {
          "type": "string",
          "minLength": 1
        },
        "queryParams": {
          "type": "object",
          "required": [
            "excludeStaff"
          ],
          "properties": {
            "excludeStaff": {
              "type": "boolean"
            }
          }
        }
      }
    }
  }
}

示例数据

[
  {
    "organisationName": "a",
    "organisationId": "a",
    "users": [
      {
        "nameWITHERROR": "a"/*e.g. name should be required*/,
        "email": "rob@test.com",
        "reports": [
          {
            "reportType": "org-weekly1"/*e.g. this should be an enum*/,
            "reportWeekEndDay": 6,
            "sendDay": 6,
            "sendHour": 6,
            "locations": [
              "abc",
              "bcd"
            ],
            "widgets": [
              {
                "widgetType": "table-all-locations",
                "comparisonType": "3-and-1-month-weekly-avg",
                "metrics": [
                  {
                    "title": "test",
                    "type": "FootFallAndMovement1"/*e.g. this enum should fail*/,
                    "endpoint": "/test",
                    "queryParams": {
                      "excludeStaff": true
                    }
                  },
                  {
                    "title": "test",
                    "type": "Engagement",
                    "endpoint": "/test",
                    "queryParams": {
                      "excludeStaff": true,
                      "contexts": {
                        "type": "taxonomy",
                        "value": "tests"
                      }
                    }
                  },
                  {
                    "title": "test",
                    "type": "Occupancy",
                    "endpoint": "/test",
                    "queryParams": {
                      "excludeStaff": true,
                      "contexts": {
                        "type": "taxonomy",
                        "value": "tests"
                      }
                    }
                  },
                  {
                    "title": "test",
                    "type": "SalesDataTransactions",
                    "endpoint": "/test"
                  },
                  {
                    "title": "test",
                    "type": "SalesDataConversion",
                    "endpoint": "/test",
                    "queryParams": {
                      "excludeStaff": true
                    }
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]
答案

[在JSONBuddy中打开JSON模式,立即向我显示您在/ definitions / Organisation / properties / users / items / ref中使用了“ ref”。由于没有找到定义,因此没有验证。JSON schema analyzer

以上是关于JSONSchema(草稿7)未验证数组的子定义的主要内容,如果未能解决你的问题,请参考以下文章

.Net使用JsonSchema验证Json

Jsonschema测试实践

Json Schema 快速入门

在 WPF 的子线程中捕获未处理的异常

如何使用 JsonSchema 进行实时客户端验证?

对于具有递归$ ref的JSONSchema,ajv验证失败