简体   繁体   English

使用AJV针对JSON模式进行API验证响应期间输出错误

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

I'm using AJV to validate an API response against JSON schema (swagger). 我正在使用AJV来针对JSON模式(swagger)验证API响应。 This is the script to do the validation: 这是执行验证的脚本:

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);

Then, I get this output: 然后,我得到以下输出:

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

1- I don't understand why ajv tell me that the "schema id" is ignored. 1-我不明白为什么ajv告诉我“ schema id”被忽略。 Why does it important? 为什么重要?

2- Why does it tell me that IdDocument/type "should be object"? 2-为什么它告诉我IdDocument /类型“应该是对象”? It is object in the response, defined as follow: 它是响应中的对象,定义如下:

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

Could someone help me to understand it? 有人可以帮我理解吗? Thanks in advance! 提前致谢!

Swagger Schema is NOT a JSON Schema, therefore you have to address a correct place in Swagger Schema ( schema.definitions.UserProfile ). Swagger Schema不是JSON Schema,因此您必须在Swagger Schema( schema.definitions.UserProfile )中解决正确的位置。

Though Swagger Definition is not 100% compatible with JSON Schema, in most of cases you can use generic validator. 尽管Swagger Definition与JSON Schema不100%兼容,但是在大多数情况下,您可以使用通用验证器。

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

You need to remove JSON.stringify , as it creates a string from your data ( string is not an object ). 您需要删除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 https://runkit.com/embed/kn4gp1fs8vat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM