【问题标题】:Why is JSON not validating against schema?为什么 JSON 不针对模式进行验证?
【发布时间】:2023-03-08 19:58:01
【问题描述】:

我在 JSON 项中的每个键上都收到“不允许附加属性”错误。下面是架构和项目。

架构:

{ “附加属性”:假, “类别”: { “管理员”:{“类型”:“布尔”} }, “用户名”:{“类型”:“字符串”}, “密码”:{“类型”:“字符串”}, “名称”:{“类型”:“字符串”}, “电子邮件”:{“类型”:“字符串”,“格式”:“电子邮件”}, “电话”:{“类型”:“字符串”}, “小时”: { “类型”:“数组”, “项目”: { “开始”:{“类型”:“字符串”,“格式”:“日期时间”}, “结束”:{“类型”:“字符串”,“格式”:“日期时间”} } } }

项目:

{ “用户名”:“emanb29”, "密码": "$2a$10$THISISAPASSWORDHASH", “名称”:“伊桑·贝尔”, “电子邮件”:“eb@ethanbell.me”, “电话”:“5555555555”, “小时”: [ { “开始”:“1998-05-29T04:00:00Z”, “结束”:“1999-05-29T04:00:00Z” }, { “开始”:“2001-05-29T10:20:00Z”, “结束”:“2001-05-29T22:20:00Z” } ], “类别”: { “管理员”:是的 } }

【问题讨论】:

标签: json jsonschema


【解决方案1】:

您的架构似乎可疑。从 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"}
        }
      }
    }
}

【讨论】:

  • 这似乎已经解决了;谢谢!我误解了“属性”子对象的必要性。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-02
  • 1970-01-01
  • 2016-06-20
  • 2017-01-09
  • 1970-01-01
  • 2014-01-08
  • 1970-01-01
相关资源
最近更新 更多