JSON Schema 4恰好验证一个定义

时间:2019-06-13 14:20:19

标签: aws-api-gateway jsonschema

我希望我的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网关的模型,但它拒绝了我发送的所有内容。错误在哪里?

1 个答案:

答案 0 :(得分:1)

struct ContentView : View { var body: some View { VStack(alignment: .center, spacing: 24) { SomeViewRepresentable() .background(Color.gray) HStack { Button(action: { print("SwiftUI: Button tapped") // Call func in SomeView() }) { Text("Tap Here") } } } } } 为假是您的问题。

它不能“透视” additionalPropertiesoneOf引用。

  

如果“ additionalProperties”具有布尔值false ...

     

在这种情况下,实例的验证取决于属性   一组“属性”和“ patternProperties”。

https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.4.4.4

关于它的工作原理还有更多说明,我们在第5草案之后对其进行了澄清,但从本质上讲...

$ref适用于additionalProperties中与properties相同的模式对象级别中未定义的所有属性。

由于您的架构仅包含additionalProperties且未定义additionalProperties,因此所有属性都会导致验证失败。

您可以通过定义属性来解决此问题,其中每个属性的值为空模式。从草案5开始,您可以使用properties作为值,因为truetrue是有效的“方案”。

相关问题