【问题标题】:How to validate dynamic API response (Schema validation) in Postman如何在 Postman 中验证动态 API 响应(模式验证)
【发布时间】:2023-02-03 15:39:32
【问题描述】:

您好,我有一个发送动态响应的 API,我想验证该响应的模式。以下是我要验证的响应。

{
    "Data": [
        {
            "CustomerName": "BeautyProductions",
  
            "Websites": {
                "storeone": {
                    "Keyone":"testvalueone",
                    "Keytwo":"testvalue two"
                }
            }
        }
    ]
}

但问题是网站的数量有时会像以下一样增加

{
    "Data": [
        {
            "CustomerName": "BeautyProductions",
  
            "Websites": {
                "storeone": {
                    "Keyone":"testvalueone",
                    "Keytwo":"testvalue two"
                },
                "storetwo": {
                    "Keyone":"testvaluestoretwo",
                    "Keytwo":"testvaluestoretwonow"
                }
            }
        }
    ]
}

我尝试按以下方式验证架构

var schema = {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "Data": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "CustomerName": {
              "type": "string"
            },
            "Websites": {
              "type": "object",
              "properties": {
                "storeone": {
                  "type": "object",
                  "properties": {
                    "Keyone": {
                      "type": "string"
                    },
                    "Keytwo": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "Keyone",
                    "Keytwo"
                  ]
                }
              },
              "required": [
                "storeone"
              ]
            }
          },
          "required": [
            "CustomerName",
            "Websites"
          ]
        }
      ]
    }
  },
  "required": [
    "Data"
  ]
}

var json = pm.response.json()
pm.test('shcema is valid', function(){
    pm.expect(tv4.validate(json, schema)).to.be.true;
})

但是当网站数量增加时它会失败。所以,我想知道如何验证这一点? 谢谢你

【问题讨论】:

    标签: arrays json api schema


    【解决方案1】:

    我不知道“存储”字段是否是必需的,但您可以使用“additionalProperties”。 “additionalProperties”字段允许您为对象中未在属性字段中明确定义的属性指定模式。 如果可行,请尝试以下架构:

    var schema = {
      "$schema": "http://json-schema.org/draft-04/schema#",
      "type": "object",
      "properties": {
        "Data": {
          "type": "array",
          "items": [
            {
              "type": "object",
              "properties": {
                "CustomerName": {
                  "type": "string"
                },
                "Websites": {
                  "type": "object",
                  "additionalProperties": {
                    "type": "object",
                    "properties": {
                      "Keyone": {
                        "type": "string"
                      },
                      "Keytwo": {
                        "type": "string"
                      }
                    },
                    "required": [
                      "Keyone",
                      "Keytwo"
                    ]
                  }
                }
              },
              "required": [
                "CustomerName",
                "Websites"
              ]
            }
          ]
        }
      },
      "required": [
        "Data"
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-11
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 2019-11-17
      • 2011-04-30
      • 1970-01-01
      相关资源
      最近更新 更多