JSON文档无法在JSON Schema上进行验证

时间:2017-01-30 15:19:51

标签: json jsonschema json-schema-validator

Json schema和Json Document下面都是有效的json。 它只是因为我无法获得与json模式相关的有效Json文档。

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

Json Schema

JSFiddle Link

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"
    ]
  }
}

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

问题出在additionalProperties的位置上。 因为用户是具有大量对象的类型数组。假设项目的属性具有additionalProperties而不是包装器“users”:{}但是“item”:{}

解决方案

Json Schema

{
  "$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"
    }]
  }
]