【问题标题】:Json schema not validating required attributeJson 模式未验证必需的属性
【发布时间】:2017-02-06 11:51:33
【问题描述】:

我正在编写以下 Json Schema:

{
  "$schema": "http://json-schema.org/schema#",
  "title": "Layout",
  "description": "The layout created by the user",
  "type": "object",
  "definitions": {
    "stdAttribute": {
      "type": "object",
      "properties": {
        "attributeValue": {
          "type": "object"
        },
        "attributeName": {
          "type": "string"
        }
      }
    },
    "stdItem": {
      "type": "object",
      "required" : ["stdAttributes"],
      "properties": {
        "stdType": {
          "enum": [
            "CONTAINER",
            "TEXT",
            "TEXTAREA",
            "BUTTON",
            "LABEL",
            "IMAGE",
            "MARCIMAGE",
            "DATA",
            "SELECT",
            "TABLE"
          ]
        },
        "stdAttributes": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/stdAttribute"
          },
          "minItems": 1
        },
        "children": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/stdItem"
          }
        }
      }
    }
  }
}

当我设置以下数据时:

{
    "stdItem": {
      "stdType": "CONTAINER",
      "stdAttributes": [],
      "children": []
  }
}

验证器说没有错误,但是在架构中我使用了一个 minItems 和一个对 "StdAttributes" 中的 "StdAttribute" 架构的引用。。 p>

我尝试在基本架构中定义此属性,但验证器说同样的话。

我应该如何验证“StdAttributes”中项目的类型和数量?

我正在使用 Java 验证器。

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    您在顶层缺少properties 属性。现在,您的架构唯一要验证的是您的数据是一个对象。 definitions 本身不会验证任何内容。它只是一个存放可以在您的架构中引用的架构的地方。以下是您必须添加到架构根目录才能获得预期结果的最低要求。

    "properties": {
      "stdItem": { "$ref": "#/definitions/stdItem" }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 2015-10-20
      • 2018-11-29
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多