简体   繁体   English

根据JSON模式验证JS测试

[英]Validating JS Tests against a JSON Schema

I have an API which returns response in the format 我有一个API,该API以以下格式返回响应

[
{"id": 12345,
"value": "some_string",
"practice_id": "12344"},

{"id": 12346,
"value": "some_other_string",
"practice_id": "12345"},
]

I am testing that the response validates a specific JSON-Schema, and my schema test is 我正在测试响应是否验证了特定的JSON模式,而我的模式测试是

response.body.should.have.schema({
        type: 'array',
        required: ['id', 'value', 'practice_id'],
        properties: {
            id: {
                type: 'number',
            },
            value: {
                type: 'string',
            },
            practice_id: {
                type: 'string',
                minLength: 5,
            }            
        }
    });

The issue is that the test passes even if I change the type of id to string or change the value of practice_id to number , which is not correct. 问题是即使我将id的类型更改为string或将Practice_id的值更改为number ,测试仍然通过,这是不正确的。

What am I doing wrong here? 我在这里做错了什么? I am using Postman-BDD to validate the responses. 我正在使用Postman-BDD来验证响应。

I guess your schema should be more like this: 我猜你的架构应该更像这样:

{
  "type": "array",
  "items":
  {
    "required":
    [
        "id",
        "value",
        "practice_id"
    ],
    "properties":
    {
        "id":
        {
            "type": "number"
        },
        "value":
        {
            "type": "string"
        },
        "practice_id":
        {
            "type": "string",
            "minLength": 5
        }
    }
  }
}

You are missing the "items" keywords to actually define the content of the array. 您缺少“ items”关键字来实际定义数组的内容。 And this schema also gives an error in JSONBuddy on validating some sample data: 而且此架构还会在验证某些样本数据时给JSONBuddy一个错误: 在此处输入图片说明

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

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