簡體   English   中英

使用AJV針對JSON模式進行API驗證響應期間輸出錯誤

[英]Wrong output during API validation response against JSON schema using AJV

我正在使用AJV來針對JSON模式(swagger)驗證API響應。 這是執行驗證的腳本:

var Ajv = require('ajv');
var ajv = new Ajv();

var schema = {
    "paths": {
      "/users": {
        "get": {
          "security": [
            {
              "3_legged": [
                "userprofile-search"
              ]
            }
          ],
          "parameters": [
            {
              "$ref": "#/parameters/IdentitiesId"
            },
            {
              "$ref": "#/parameters/IdDocumentValue"
            },
            {
              "$ref": "#/parameters/IdDocumentType"
            }
          ],          
          "responses": {
            "200": {
              "headers": {
                "x-correlator": {
                  "type": "string",
                  "format": "uuid",
                }
              },
              "schema": {
                "type": "array",
                "items": {
                  "$ref": "#/definitions/UserProfile"
                }
              }
            }
          }
        }
      },
      "/users/{user_id}": {
        "get": {
          "security": [
            {
              "3_legged": [
                "userprofile-read"
              ]
            }
          ],
          "tags": [
            "users"
          ],
          "operationId": "getUserProfileInfo",
          "parameters": [
            {
              "$ref": "#/parameters/UserId"
            }
          ],          
          "responses": {
            "200": {
              "description": "OK",
              "headers": {
                "x-correlator": {
                  "type": "string",
                  "format": "uuid",
                  "description": "Correlation id"
                }
              },
              "schema": {
                "$ref": "#/definitions/UserProfile"
              },
              "examples": {
                "application/json": {
                  "id": "A000-0000-0001",
                  "name": "Andrés Iniesta",
                  "id_document": {
                    "country": "ES",
                    "type": "NIF",
                    "value": "value"
                  },
                  "identities": [
                    {
                      "type": "email",
                      "id": "id",
                      "services": [
                        "iptv",
                        "email"
                      ]
                    },
                    {
                      "type": "phone_number",
                      "id": "id",
                      "services": [
                        "mobile"
                      ]
                    },
                    {
                      "type": "phone_number",
                      "id": "id",
                      "services": [
                        "mobile"
                      ]
                    },
                    {
                      "type": "phone_number",
                      "id": "id",
                      "services": [
                        "landline",
                        "broadband"
                      ]
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
  "definitions": {
    "UserProfile": {
      "type": "object",
      "required": [
        "id",
        "name",
        "identities"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "id_document": {
          "$ref": "common.json#/definitions/IdDocument"
        },
        "identities": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Identity"
          }
        }
      }
    },
    "Identity": {
      "type": "object",
      "required": [
        "id",
        "services",
        "type"
      ],
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "phone_number",
            "email",
            "uid"
          ]
        },
        "services": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": [
              "mobile",
              "invoicing"
            ]
          }
        },
        "id": {
          "type": "string"
        }
      }
    }
  }
}

var common = {
  "definitions": {
    "MoneyAmount": {
      "type": "object",
      "properties": {
        "amount": {
          "type": "number"
        }
      }
    },
    "IdDocument": {
      "type": "object",
      "required": [
        "country",
        "type",
        "value"
      ],
      "properties": {
        "country": {
          "type": "string"
        },
        "type": {
          "type": "string"
        },
        "value": {
          "type": "string"
        }
      }
    }
  }
}

var response={
    "id": "123456789",
    "name": "pruebas trocafone prepago",
    "id_document": {
        "country": "ARG",
        "type": "P",
        "value": "15042016"
    },
    "identities": [
        {
            "type": "uid",
            "services": [
                "invoicing"
            ],
            "id": "511644813"
        },
        {
            "type": "phone_number",
            "services": [
                "mobile"
            ],
            "id": "00123456789"
        },
        {
            "type": "email",
            "services": [
                "email"
            ],
            "id": ""
        }
    ]
}

ajv.addSchema(schema, 'user_profile.json');
ajv.addSchema(common, 'common.json');

var testajv = ajv.compile({ $ref: 'common.json#/definitions/IdDocument' });

console.log(testajv(JSON.stringify(response)), testajv.errors);

然后,我得到以下輸出:

schema id ignored A000-0000-0001
false [ { keyword: 'type',
    dataPath: '',
    schemaPath: 'common.json#/definitions/IdDocument/type',
    params: { type: 'object' },
    message: 'should be object' } ]

1-我不明白為什么ajv告訴我“ schema id”被忽略。 為什么重要?

2-為什么它告訴我IdDocument /類型“應該是對象”? 它是響應中的對象,定義如下:

"id_document": {
    "country": "ARG",
    "type": "P",
    "value": "15042016"
}

有人可以幫我理解嗎? 提前致謝!

Swagger Schema不是JSON Schema,因此您必須在Swagger Schema( schema.definitions.UserProfile )中解決正確的位置。

盡管Swagger Definition與JSON Schema不100%兼容,但是在大多數情況下,您可以使用通用驗證器。

更多信息: https : //github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schemaObject

您需要刪除JSON.stringify ,因為它會從數據中創建一個stringstring不是object )。

ajv.addSchema(schema.definitions.UserProfile, 'user_profile.json');
ajv.addSchema(common, 'common.json');

var testajv = ajv.compile({ $ref: 'common.json#/definitions/IdDocument' });

console.log(testajv(response), ajv.errorsText(testajv.errors));

https://runkit.com/embed/kn4gp1fs8vat

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM