【发布时间】:2017-07-11 12:28:30
【问题描述】:
考虑一个带有端点的 API,您可以在其中将参数 foo 作为 URL 路径的一部分传递,并在 POST 请求的正文中将一些参数作为 json 传递。
- 此 uri 参数
foo必须具有值fooBar和fooBaz之一。否则,请求会生成 404。 - 如果
foo的值为fooBar,则需要body 属性bar。 - 如果
foo的值为fooBaz,则需要body 属性baz。
如何为这样的端点指定 jsonSchema?
此类请求的示例是:
POST /path/to/endpoint/fooBar HTTP/1.1
Accept: application/json
Content-Type: application/json
Content-Length: 20
{"bar":"something"}
到目前为止,我根据this 和that 提出了以下内容,但我不知道这是否正确。
{
"$schema": "http://json-schema.org/draft-03/schema#",
"id": "http://json-schema.org/draft-03/schema#",
"definitions": {
"foo": { "enum": ["fooBar", "fooBaz"] }
},
"type": "object",
"properties": {
"foo": { "$ref": "#/definitions/foo" },
"bar": { "type": "string" },
"baz": { "type": "string" }
},
"links": [{
"rel": "self",
"href": "/path/to/endpoint/{foo}",
"hrefSchema": {
"properties": {
"foo": {"$ref": "#/definitions/foo"}
}
}
}],
"anyOf": [
{
"properties": {
"foo": { "enum": ["fooBar"] }
},
"required": ["bar"]
},
{
"properties": {
"foo": { "enum": ["fooBaz"] }
},
"required": ["baz"]
},
]
}
【问题讨论】:
标签: jsonschema