postman中json模式的ajv验证错误

postman中json模式的ajv验证错误,json,postman,jsonschema,ajv,Json,Postman,Jsonschema,Ajv,我有: { "data": { "regex": "some regex", "validationMessage": "some validation message" } } 我使用它来构建json模式 初始化如下所示: var Ajv = require('ajv'), ajv = new Ajv({logger: console}), schema = { "definitions": {}, "$schema":

我有:

{
    "data": {
        "regex": "some regex",
        "validationMessage": "some validation message"
    }
}
我使用它来构建json模式

初始化如下所示:

var Ajv = require('ajv'),
    ajv = new Ajv({logger: console}),
    schema = {
  "definitions": {},
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://example.com/root.json",
  "type": "object",
  "properties": {
    "data": {
      "$id": "#/properties/data",
      "type": "object",
      "properties": {
        "regex": {
          "$id": "#/properties/data/properties/regex",
          "type": "string",
          "pattern": "^(.*)$"
        },
        "validationMessage": {
          "$id": "#/properties/data/properties/validationMessage",
          "type": "string",
          "pattern": "^(.*)$"
        }
      }
    }
  }
};
然后我想检查json模式是否有效

pm.test('Schema is valid', function() {
    pm.expect(ajv.validate(schema, {alpha: 123})).to.be.true;
});
我看到考试通过了。 发生了什么?为什么模式是有效的


此外,我将用
JSON.parse(responseBody)
替换
{alpha:123}
,您可以尝试将其更改为如下内容:

var Ajv = require('ajv'),
    ajv = new Ajv({logger: console, allErrors: true}),
    schema = {
      "type": "object",
      "required": [
        "data"
      ],
      "properties": {
        "data": {
          "type": "object",
          "required": [
            "regex",
            "validationMessage"
          ],
          "properties": {
            "regex": {
              "type": "string",
              "pattern": "^(.*)$"
            },
            "validationMessage": {
              "type": "string",
              "pattern": "^(.*)$"
            }
          }
        }
      }
    };

pm.test('Schema is valid', function() {
    pm.expect(ajv.validate(schema, { alpha: 123 }), JSON.stringify(ajv.errors)).to.be.true;
});
我将
allErrors
选项添加到
Ajv
中,并在测试中暴露这些错误。我还稍微修改了您的模式,添加了对象所需的键


我测试了这将是一个在测试中硬编码的对象,但也有一个模拟响应。

你在《邮差》中到底是如何使用它的?作为OpenAPI定义文档的一部分?我可以告诉您为什么实例对架构有效,但我无法告诉您需要更改什么,除非您告诉我为什么您希望验证失败。=]我正在使用Postman测试我的API查询,我想检查所有字段(架构中的字段)是否都存在于响应中。@A.Gladkiy我提供的答案是否有帮助?是的,谢谢,我只需要包括必填字段,它工作正常。