【发布时间】:2019-08-19 16:15:53
【问题描述】:
背景
我正在使用ajv with postman 进行 json 架构验证。它运行良好,但是它只为我提供了整个架构验证的单个失败/通过结果,而不是确切的失败键/值对:
var Ajv = require('ajv'),
ajv = new Ajv({logger: console}),
schema = {
"properties": {
"data" : {
"type": "object",
"properties" : {
"categories" : {
"type": "array",
"items" : [
{"type": "object",
"properties" : {
"id": {"type": "number"},
"ref": {"type": ['null', 'string']},
"parent_id": {"type": ['null', 'number']},
"image": {"type": ['null', 'string']},
...
pm.test('Schema is valid', function() {
var data = pm.response.json()['data'];
pm.expect(ajv.validate(schema, {data: data})).to.be.true;
});
问题
如何让 avj/postman 返回完全错误的密钥/对验证?
更新
为此,必须像这样声明 avj 对象:
var Ajv = require('ajv'),
ajv = new Ajv({logger: console,
allErrors: true,
verbose: true
}),
【问题讨论】:
-
有一个
errors对象保存信息 -pm.expect(ajv.validate(accessSchema, pm.response.json()), JSON.stringify(ajv.errors[0].message)).to.be.true;不确定答案,但其中有一些东西可以公开。 -
完美!如果您将评论作为答案,我会将其标记为正确
-
会的,我很想看看你是如何使用它的。任何文章的链接将不胜感激:)
标签: json validation postman