pro*_*ler 4 json postman json-schema-validator
我在邮递员中遇到数组 json 模式验证的问题。
var schema = {
"type": "array",
"items": [{
"id": {
"type":"long"
},
"name": {
"type":"string"
},
"email": {
"type":"string"
}
}]
};
pm.test('Response schema type nodes verification', function() {
pm.expect(tv4.validate(pm.response.json(), schema)).to.be.true;
});
Run Code Online (Sandbox Code Playgroud)
响应主体是:
[
{
"id": 1,
"name": "test1",
"email": "a@a.com"
},
{
"id": 2,
"name": "test2",
"email": "a@a.com"
},
.
.
.
]
Run Code Online (Sandbox Code Playgroud)
我一直通过结果。我也试过删除[].
问题出在哪儿?
有问题使用的架构不正确,您需要将数组中的项目类型定义为object。正确的 JSON 架构如下所示:
var schema = {
"type": "array",
"items": [{
type: "object",
properties:{
"id": {
"type":"integer"
},
"name": {
"type":"string"
},
"email": {
"type":"string"
}
}
}]
};
pm.test('Response schema type nodes verification', function() {
pm.expect(tv4.validate(pm.response.json(), schema)).to.be.true;
});
Run Code Online (Sandbox Code Playgroud)
请注意,JSON Schema 中只有 2 种数字类型:integer和number. 没有类型 as long。
| 归档时间: |
|
| 查看次数: |
3314 次 |
| 最近记录: |