为什么此 JSON 不针对此架构进行验证?
Why doesn't this JSON validate against this schema?
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
{
"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
{
"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"
}
根据架构验证文档时出现以下错误:
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"]
}
]
}
我知道它正在引入定义,因为如果我使引用无效,它将引发错误。
问题可能是您的架构无效。 date-time
和 email
不是 type
关键字的有效值。您需要改为执行以下操作。
{ "type": "string", "format": "date-time" }
和
{ "type": "string", "format": "email" }
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
{
"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
{
"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"
}
根据架构验证文档时出现以下错误:
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"]
}
]
}
我知道它正在引入定义,因为如果我使引用无效,它将引发错误。
问题可能是您的架构无效。 date-time
和 email
不是 type
关键字的有效值。您需要改为执行以下操作。
{ "type": "string", "format": "date-time" }
和
{ "type": "string", "format": "email" }