【问题标题】:Problem with schema validation using Postman使用 Postman 进行模式验证的问题
【发布时间】:2020-07-11 22:11:00
【问题描述】:

我的请求正文:

[
  {
    "postId": 1,
    "id": 1,
    "name": "name abc",
    "email": "Eliseo@gardner.biz",
    "body": "something"
  },
...
]

我正在尝试如下验证:

var schema = {
  "type": "array",
  "properties": {
    "postId": {
      "type": "integer"
    },
    "id": {
      "type": "integer"
    },
     "name": {
      "type": "string"
    },
    "email": {
      "type": "string",
      "pattern": "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$"
    },
    "body": {
      "type": "string"
    }
  },
  "required": [
    "postId",
    "id",
    "name",
    "email",
    "body"
  ]
};

pm.test('Schemat jest poprawny', function() {
  pm.expect(tv4.validate(jsonData, schema)).to.be.true;
});

即使我更改了字符串的 id 类型或无效的电子邮件模式,测试也可以。

那段代码有什么问题?

【问题讨论】:

    标签: validation postman schema


    【解决方案1】:

    我建议不要使用 tv4 进行架构验证,而是使用内置的 jsonSchema 函数,因为它使用 AJV

    除此之外,您的架构看起来不正确,并且缺少针对 object 的验证,看起来它正在针对 array 进行验证。

    这可能会对您有所帮助:

    let schema = {
        "type": "array",
        "items": {
            "type": "object",
            "required": [
                "postId",
                "id",
                "name",
                "email",
                "body"
            ],
            "properties": {
                "postId": {
                    "type": "integer"
                },
                "id": {
                    "type": "integer"
                },
                "name": {
                    "type": "string"
                },
                "email": {
                    "type": "string",
                    "pattern": "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$"
                },
                "body": {
                    "type": "string"
                }
            }
        }
    }
    
    pm.test("Schemat jest poprawny", () => {
        pm.response.to.have.jsonSchema(schema)
    })
    

    【讨论】:

    • 你能否接受这个答案,如果对你有帮助,请点赞。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2019-12-21
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    相关资源
    最近更新 更多