为什么JSON没有验证架构?

时间:2013-11-19 01:04:57

标签: json jsonschema

我在JSON项目的每个键上都出现“不允许其他属性”错误。下面是架构和项目。

架构:

    {
      "additionalProperties": false,
      "category": {
        "admin": {"type": "boolean"}
      },
      "username": {"type": "string"},
      "password": {"type": "string"},
      "name": {"type": "string"},
      "email": {"type": "string", "format": "email"},
      "phone": {"type": "string"},
      "hours": {
        "type": "array",
        "items": {
          "start": {"type": "string", "format": "date-time"},
          "end": {"type": "string", "format": "date-time"}
        }
      }
    }

档案:

    {
        "username": "emanb29",
        "password": "$2a$10$THISISAPASSWORDHASH",
        "name": "Ethan Bell",
        "email": "eb@ethanbell.me",
        "phone": "5555555555",
        "hours": [
            {
                "start": "1998-05-29T04:00:00Z",
                "end": "1999-05-29T04:00:00Z"
            },
            {
                "start": "2001-05-29T10:20:00Z",
                "end": "2001-05-29T22:20:00Z"
            }
        ],
        "category": {
            "admin": true
        }
    }

1 个答案:

答案 0 :(得分:2)

您的架构似乎很可疑。使用jsonschemalint.com上的示例,我在根目录中为您的属性创建了一个properties容器,在根目录中添加了descriptiontype,并将additionalProperties移动到根目录好。

这会在jsonschemalint.com上验证您的项目:

{
    "description": "StackOverflow test schema", 
    "type": "object", 
    "additionalProperties": false,
    "properties":     {
      "category": {
        "admin": {"type": "boolean"}
      },
      "username": {"type": "string"},
      "password": {"type": "string"},
      "name": {"type": "string"},
      "email": {"type": "string", "format": "email"},
      "phone": {"type": "string"},
      "hours": {
        "type": "array",
        "items": {
          "start": {"type": "string", "format": "date-time"},
          "end": {"type": "string", "format": "date-time"}
        }
      }
    }
}