【问题标题】:JSON document fails validation on JSON SchemaJSON 文档在 JSON Schema 上的验证失败
【发布时间】:2017-01-30 15:19:51
【问题描述】:

下面的 Json 模式和 Json 文档都是有效的 json。 只是我无法获得与 json 架构相关的有效 Json 文档。

我收到错误消息:不应该有其他属性

Json 架构

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Movies Schema",
  "description": "Movies schema containing ratings and genres",
  "type": "array",
  "items": {
    "type": "object",
    "additionalProperties": false,
    "properties": {
      "id": {
        "type": "number"
      },
      "title": {
        "type": "string"
      },
      "release_date": {
        "type": "string"
      },
      "video": {
        "type": "string"
      },
      "IMDBURL": {
        "type": "string"
      },
      "genres": {
        "type": "array"
      }
    },
    "required": [
      "id",
      "title",
      "release_date",
      "video",
      "IMDBURL",
      "genres"
    ]
  },
  "users": {
    "type": "object",
    "additionalProperties": false,
    "properties": {
      "user_id": {
        "type": "number"
      },
      "ratings": {
        "type": "number"
      },
      "timestamps": {
        "type": "string"
      }
    },
    "required": [
      "user_id",
      "ratings",
      "timestamps"
    ]
  }
}

JSON 文档

[
  {
    "id": 1,
    "title": "Kung Fu Panda",
    "release_date": "01-01-2001",
    "video": "",
    "IMDBURL": "link.com",
    "genres": [
      "abc",
      "def"
    ],
    "users": {
      "user_id": 2,
      "ratings": 3,
      "timestamps": "2342478"
    }
  }
]

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    架构中缺少"users" 属性,因此它是一个附加属性,因此它违反了"additionalProperties": false 设置。

    如果您在架构中定义 "users" 属性,那么您的文档将是有效的。

    【讨论】:

    • 请检查它是否存在,滚动到底部。
    • 在您的 JSON 对象中,“用户”是“电影”的一部分,因此在您的架构中,您必须在电影架构中添加“用户”作为字段
    【解决方案2】:

    问题在于附加属性的位置。 因为用户是具有大量对象的数组类型。假设项目的属性具有附加属性,而不是包装器“用户”:{},而是“项目”:{}

    解决方案

    Json 架构

    {
      "$schema": "http://json-schema.org/draft-04/schema#",
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "id": {
            "type": "integer"
          },
          "title": {
            "type": "string"
          },
          "release_date": {
            "type": "string"
          },
          "video": {
            "type": "string"
          },
          "IMDBURL": {
            "type": "string"
          },
          "genres": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "users": {
            "type": "array",
            "items": {
              "type": "object",
              "additionalProperties": false,
              "properties": {
                "user_id": {
                  "type": "integer"
                },
                "ratings": {
                  "type": "integer"
                },
                "timestamps": {
                  "type": "string"
                }
              },
              "required": [
                "user_id",
                "ratings",
                "timestamps"
              ]
            }
          }
        },
        "required": [
          "id",
          "title",
          "release_date",
          "video",
          "IMDBURL",
          "genres",
          "users"
        ]
      }
    }
    

    Json 文档

    [
      {
        "id": 1,
        "title": "Kung Fu Panda",
        "release_date": "01-01-2000",
        "video": "",
        "IMDBURL": "link.com",
        "genres": [
          "abc",
          "def"
        ],
        "users": [{
          "user_id": 2,
          "ratings": 3,
          "timestamps": "2342478"
        },
    {
          "user_id": 2,
          "ratings": 3,
          "timestamps": "2342478"
        },
    {
          "user_id": 2,
          "ratings": 3,
          "timestamps": "2342478"
        },
    {
          "user_id": 2,
          "ratings": 3,
          "timestamps": "2342478"
        }]
      },
    {
        "id": 1,
        "title": "Kung Fu Panda",
        "release_date": "01-01-2000",
        "video": "",
        "IMDBURL": "link.com",
        "genres": [
          "abc",
          "def"
        ],
        "users": [{
          "user_id": 2,
          "ratings": 3,
          "timestamps": "2342478"
        },
    {
          "user_id": 2,
          "ratings": 3,
          "timestamps": "2342478"
        },
    {
          "user_id": 2,
          "ratings": 3,
          "timestamps": "2342478"
        },
    {
          "user_id": 2,
          "ratings": 3,
          "timestamps": "2342478"
        }]
      },
    {
        "id": 1,
        "title": "Kung Fu Panda",
        "release_date": "01-01-2000",
        "video": "",
        "IMDBURL": "link.com",
        "genres": [
          "abc",
          "def"
        ],
        "users": [{
          "user_id": 2,
          "ratings": 3,
          "timestamps": "2342478"
        },
    {
          "user_id": 2,
          "ratings": 3,
          "timestamps": "2342478"
        },
    {
          "user_id": 2,
          "ratings": 3,
          "timestamps": "2342478"
        },
    {
          "user_id": 2,
          "ratings": 3,
          "timestamps": "2342478"
        }]
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 2018-10-03
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 2021-05-06
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 2015-06-18
      相关资源
      最近更新 更多