简体   繁体   English

JSON模式验证在Postman中失败,即使它是有效的

[英]JSON schema validation is failing in Postman even if it's valid

I need to validate following response against JSON schema. 我需要针对JSON模式验证以下响应。 The problem is it's always failing even if schema is valid. 问题是,即使架构有效,也总是失败。

{
    "items": [
        {
            "uuid": "f68ad4ba-a11e-485d-a2d7-17b9b07bd8d3",
            "name": "Code",
            "type": "app_code",
            "description": "Code 1",
            "content_type": "application/javascript",
            "audit": {
                "created_date": "2017-11-02T00:16:58.000Z",
                "created_by": "e7c97dc1-08eb-45ef-b883-d100553bac5c"
            }
        },
        {
            "uuid": "3c9e59b0-f6c7-4788-8a14-0b3e99fc4306",
            "name": "Object demo 2",
            "type": "app_code",
            "description": "Object demo 2 description",
            "content_type": "application/javascript",
            "audit": {
                "created_date": "2017-11-02T13:48:22.000Z",
                "created_by": "e7c97dc1-08eb-45ef-b883-d100553bac5c"
            }
        },
        {
            "uuid": "e2f54e6c-a158-4f43-a332-1c99bb76684e",
            "name": "toolbox_test_results",
            "type": "toolbox_tests",
            "description": "This is snapshots for React-OSS Toolbox",
            "content_type": "application/json",
            "audit": {
                "created_date": "2017-11-07T11:29:02.000Z",
                "created_by": "e7c97dc1-08eb-45ef-b883-d100553bac5c"
            }
        }
    ],
    "metadata": {
        "total": 124,
        "count": 3
    },
    "links": [
        {
            "rel": "self",
            "href": "http://microsvcs.star2star.net:10010/users/e7c97dc1-08eb-45ef-b883-d100553bac5c/objects?load_content=false&limit=3&offset=0"
        },
        {
            "rel": "first",
            "href": "http://microsvcs.star2star.net:10010/users/e7c97dc1-08eb-45ef-b883-d100553bac5c/objects?load_content=false&limit=3&offset=0"
        },
        {
            "rel": "next",
            "href": "http://microsvcs.star2star.net:10010/users/e7c97dc1-08eb-45ef-b883-d100553bac5c/objects?load_content=false&limit=3&offset=3"
        },
        {
            "rel": "last",
            "href": "http://microsvcs.star2star.net:10010/users/e7c97dc1-08eb-45ef-b883-d100553bac5c/objects?load_content=false&limit=3&offset=123"
        }
    ]
}

I use following code for validation in Postman: 我在邮递员中使用以下代码进行验证:

// Define the JSON Schema //定义JSON模式

const objectSchema = {
  "items": [
    {
      "audit": {
        "created_by": "string",
        "created_date": "string",
        "updated_by": "string",
        "updated_date": "string"
      },
      "content": {},
      "content_type": "string",
      "description": "string",
      "name": "string",
      "type": "string",
      "uuid": "string"
    }
  ],
  "links": [
    {
      "href": "string",
      "rel": "string",
      "templated": true
    }
  ],
  "metadata": {}
};


pm.test("JSON schema validation", function() {
  var responseData = JSON.parse(responseBody);
  var result = tv4.validate(responseData, objectSchema, false, true);
  if (result !== true) {
      console.log('Schema validation failed:', tv4.error);
  }
  pm.expect(result).to.be.true;
  console.log(JSON.stringify(result));
});

How it's possible to get the detailed error in console (for example: which field has wrong type or missing)? 如何在控制台中获得详细的错误(例如:哪个字段的类型错误或丢失)? Actual error from console: 来自控制台的实际错误:

message:"Unknown property (not in schema)"
name:"ValidationError"
type:"Error

" Thank you in advance for answers! ”预先感谢您的回答!

Elaborating on @Pavlo's comment: 详细阐述@Pavlo的评论:

In your test: 在您的测试中:

pm.test("JSON schema validation", function() {
  var responseData = JSON.parse(responseBody);
  var result = tv4.validate(responseData, objectSchema, false, true);
  if (result !== true) {
      console.log('Schema validation failed:', tv4.error);
  }
  pm.expect(result).to.be.true;
  console.log(JSON.stringify(result));
});

the line var responseData = JSON.parse(responseBody); 这行var responseData = JSON.parse(responseBody); should be replaced with: 应替换为:

var responseData = pm.response.json();

Also, from this answer you can add the following line to get more information about where the schema failed: 此外,从此答案中,您可以添加以下行以获取有关架构失败位置的更多信息:

console.log(tv4.error.dataPath);

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

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