【发布时间】:2018-02-01 11:57:30
【问题描述】:
我发现很难理解如何使用不同的验证集来验证不同类型的响应。我只是放了一个示例代码,然后尝试解释它可能有意义。
示例数据集:
responses: [
{ type: 'user', age: 5 }
{ type: 'admi', auth: {...} }
]
json 架构示例:
{
"definitions": {
"user": {
"type": "object",
"properties": {
"type": { "type": "string" },
"age": { "type": "number" }
}
"required": ["age"]
},
"admin": {
"type": "object",
"properties": {
"type": { "type": "string" },
"auth": { "type": "object" }
}
"required": ["auth"]
}
},
"responses": {
"type": "array",
"anyOf": [
{ "$ref": "#/definitions/user" }
{ "$ref": "#/definitions/admi" }
]
}
}
如何根据类型验证这些(不是string, number,而是'user', 'admi')?
【问题讨论】:
-
这里的语言有点混乱。对象的类型是您已定义的,并且您使用
anyOf走在正确的轨道上。我会尽快给出答案。
标签: json jsonschema json-schema-validator