aws-api-gateway - JSON Schema 4 仅验证一个定义
问题描述
我希望我的 HTTP 请求正文将接受如下内容:
{
"grant_type": "refresh_token", // "refresh_token" or "password"
"client_id": "my-client", // NEVER CHANGE
"refresh_token": "XXX"
}
或者
{
"grant_type": "password", // "refresh_token" or "password"
"client_id": "my-client", // NEVER CHANGE
"username": "XXX",
"password": "XXX",
}
如您所见,格式更改基于grant_type
. 所以我定义了这个模式:
{
"definitions": {
"username_and_password": {
"type": "object",
"properties": {
"grant_type": { "type": "string", "enum": ["password"] },
"client_id": { "type": "string", "enum": ["my-client"] },
"username": { "type": "string" },
"password": { "type": "string" }
},
"required": ["grant_type", "client_id", "username", "password" ]
},
"refresh_token": {
"type": "object",
"properties": {
"grant_type": { "type": "string", "enum": ["refresh_token"] },
"client_id": { "type": "string", "enum": ["my-client"] },
"refresh_token": { "type": "string" }
},
"required": [ "grant_type", "client_id", "refresh_token" ]
}
},
"oneOf": [
{ "$ref": "#/definitions/username_and_password" },
{ "$ref": "#/definitions/refresh_token" }
],
"additionalProperties": false
}
我将其用作 API Gateway 的模型,但它拒绝了我发送的所有内容。错误在哪里?
解决方案
additionalProperties
虚假是你的问题。
它不能“透视”oneOf
或$ref
引用。
如果“additionalProperties”具有布尔值 false...
在这种情况下,实例的验证取决于“properties”和“patternProperties”的属性集。
https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00#section-5.4.4.4
对它的工作原理有更多解释,我们在草案 5 之后对其进行了澄清,但本质上......
additionalProperties
适用于未properties
在与 相同的模式对象级别中定义的所有属性additionalProperties
。
因为您的架构只有additionalProperties
并且没有properties
定义,所以所有属性都会导致验证失败。
您可以通过定义属性来解决这个问题,其中每个属性的值都是空模式。从草案 5 开始,您可以将true
其用作值,因为true
和false
是有效的“模式”。
推荐阅读
- excel - VBA 中的 Rs.update 问题
- c# - 如何在xml中的c#中反序列化后创建一个对象
- java - Functionality of map activity doesn't work, when map layout is included in another activity (Android Studio)
- python - 在 Python 中遇到 String.find() 的问题
- redirect - 如果用户使用 Linux 浏览器,则重定向网站
- python - (BadRequestKeyError) 不知道问题出在哪里
- jquery - 将日期选择器中的值存储到变量中
- python - 试图在python中制作一个线脚轮
- excel - 附加 A 列的最后 4 个字符并添加到 B 列
- methods - 如何编写方法引用?