使用 Postman 时,我像这样验证 JSON 响应:
tv4.addSchema(globalSchema);
const valResult = tv4.validate(data, schema);
// schema is an object, which is a subschema from the larger globalSchema
工作正常,除了错误报告。我得到的错误对象缺少dataPath
和schemaPath
,使我的用户很难找出实际问题所在。有没有办法获得这些属性?(尝试验证结果和验证多个无济于事(
作为替代方案,我尝试了 ajv,但由于我在 draft-04 中,它给了我错误。来自他们网站的建议
var ajv = new Ajv({schemaId: 'id'});
// If you want to use both draft-04 and draft-06/07 schemas:
// var ajv = new Ajv({schemaId: 'auto'});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
不起作用,因为邮递员沙盒不允许我要求...有什么想法吗?
另请参阅:https://community.getpostman.com/t/json-schema-validation-troubles/5024
以下是我如何用邮递员验证架构以获得更详细的错误:
const schema = {
};
var jsonData = JSON.parse(responseBody);
pm.test('Checking Response Against Schema Validation', function() {
var result=tv4.validateMultiple(jsonData, schema);
console.log(result);
pm.expect(result.valid).to.be.true;
});