简体   繁体   English

为什么此JSON无法针对该架构进行验证?

[英]Why doesn't this JSON validate against this schema?

user_definition.json user_definition.json

{
  "definitions": {
    "user": {
      "type": "object",
      "properties": {
        "first_name": { "type": "string" },
        "last_name":  { "type": "string" },
        "email":      { "type": "email" }
      },
      "required": ["first_name", "last_name", "email"]
    }
  }
}

user_schema.json user_schema.json

{
  "allOf": [
    { "$ref": "../definitions/user_definition.json#/definitions/user" },
    {
      "properties": {
        "id":         { "type": "string" },
        "created_at": { "type": "date-time" },
        "updated_at": { "type": "date-time" }
      },
      "required": ["id", "created_at", "updated_at"]
    }
  ]
}

JSON JSON格式

{
  "id":"f10b9c6e-695c-11e5-bf98-c7cb6d8af9eb",
  "first_name":"Johanna",
  "last_name":"Littel",
  "email":"ora_turcotte@ratke.net",
  "created_at":"2015-10-02T23:26:00.663Z",
  "updated_at":"2015-10-02T23:26:00.663Z"
}

I get the following error when I validate the document against the schema: 当我根据架构验证文档时,出现以下错误:

expected

{"id":"f10b9c6e-695c-11e5-bf98-c7cb6d8af9eb","first_name":"Johanna","last_name":"Littel","email":"ora_turcotte@ratke.net","created_at":"2015-10-02T23:26:00.663Z","updated_at":"2015-10-02T23:26:00.663Z"}

to match schema "user_response":

       {
         "allOf": [
           { "$ref": "../definitions/user_definition.json#/definitions/user" },
           {
             "properties": {
               "id":         { "type": "string" },
               "created_at": { "type": "date-time" },
               "updated_at": { "type": "date-time" }
             },
             "required": ["id", "created_at", "updated_at"]
           }
         ]
       }

I know it's pulling in the definition because if I make the ref invalid it will throw an error. 我知道它引入了定义,因为如果我使ref无效,它将抛出错误。

The problem is probably that your schema is invalid. 问题可能是您的架构无效。 date-time and email are not valid values for the type keyword. date-timeemail不是type关键字的有效值。 You will need to do something like the following instead. 您将需要执行类似以下的操作。

{ "type": "string", "format": "date-time" }

and

{ "type": "string", "format": "email" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM