【问题标题】:Postman Json Schema validation failedPostman Json Schema 验证失败
【发布时间】:2018-10-03 07:44:06
【问题描述】:

我有这个小问题。我正在用邮递员构建 API 测试。 我的一项测试想要验证 Json 响应。

这是我收到的回复:

       {
        "comuni": [
         {
        "istat": "015002",
        "code": "A010",
        "comune": "ABBIATEGRASSO",
        "provincia": "MI",
        "cap": "20081",
        "latitude": 45.393036,
        "longitude": 8.919824,
        "soppresso": false,
        "regione": "Lombardia",
        "parte_italia": "nord",
        "is_provincia": 0,
        "nome_provincia": "Milano"
    },
    ...
    ...
    ]};

所以我收到了一组像上面这样的对象。 这是我写的测试:

            var schema = {
            "comuni" :
         [
            {
                "istat" : {
                    "type" : "Integer"
                },
                "code" : {
                    "type" : "string"
                },
                "comune" : {
                    "type" : "string"
                },
                "provincia" : {
                    "type" : "string"
                },
                "cap" : {
                    "type" : "integer"
                },
                "latitude" : {
                    "type": "Number"
                },
                "longitude" : {
                    "type": "Number"
                },
                "soppresso": {
                    "tyoe" : "boolean"
                },
                "regione" : {
                    "type" : "string"
                },
                "parte_italia": {
                    "type": "string"
                },
                "is_provincia": {
                    "type": "integer"
                },
                "nome_provincia": {
                    "type": "string"
                }

    }]
}

pm.test("JSON schema validation", function() {
  var paperwork = pm.response.json();
  var result = tv4.validate(paperwork, schema, false, true);
  if (result !== true) {
      console.log('Schema validation failed:', tv4.error);
  }
  /*console.log(tv4.error.dataPath);*/
  pm.expect(result).to.be.true;
  console.log(JSON.stringify(result));
});

但测试失败:

架构验证失败:未知属性(不在架构中)

显然我在架构上做错了什么,但我不明白是什么。

【问题讨论】:

    标签: json api automated-tests postman


    【解决方案1】:

    您的架构不正确。应该是这样的。

    {
     "description": "Any validation failures are shown in the right-hand Messages pane.",
    "type": "object",
    "properties": {
     "foo": {
      "type": "number"
    },
    "bar": {
      "type": "string",
      "enum": [
        "a",
        "b",
        "c"
        ]
     }
     }
    }
    

    数据应该是这样的,

    {
    "foo": 12345,
    "bar": "a"
    }
    

    更多examples,如数组/对象等,请参阅下面的链接。

    【讨论】:

    • 不,我没有写最后的 ]} 因为我只写了第一个对象。结果是“comuni”,它是一个对象数组,就像你在上面看到的那样
    • 当你说,你没有写]},你到底是什么意思?它是否存在于您的数据中,但您没有提出原始问题?
    • 根据 POSTMAN 使用的 json-schema4.0 版本,您的架构定义不正确。我将修改我上面的答案并尝试修改您的架构,它应该可以工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 2021-04-18
    • 2019-10-18
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多