You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Postman中使用AJV验证JSON时忽略嵌套结构问题排查

问题根源与解决方法

你的问题出在Schema的类型关键字写错了——JSON Schema规范里,类型必须是小写的字符串,你把NestedAttribute的type写成了大写的"String",AJV无法识别这个无效的类型值,直接跳过了该字段的验证逻辑。

修正后的Schema

把"String"改成小写的"string"即可:

{
  "type": "object",
  "required": [
    "MainThing"
  ],
  "properties": {
    "MainThing": {
      "type": "string",
      "description": "Some kind of string.",
      "example": "This is some kind of string."
    },
    "NestedThing": {
      "type": "object",
      "required": [
        "NestedAttribute"
      ],
      "properties": {
        "NestedAttribute": {
          "type": "string", // 这里改成小写
          "description": "A nested string.",
          "example": "This is a nested string"
        }
      }
    }
  }
}

优化测试代码(可选但推荐)

当前测试只返回验证结果,看不到具体错误信息,建议加上错误输出,方便后续排查问题:

var Ajv = require('ajv'),
    ajv = new Ajv({logger: console}),
    schema = JSON.parse(pm.variables.get("THIS_IS_MY_AWESOME_SCHEMA"));
 
pm.test('My schema is valid', function() {
    var data = pm.response.json();
    var isValid = ajv.validate(schema, data);
    pm.expect(isValid).to.be.true;
    // 如果验证失败,打印具体错误
    if (!isValid) {
        console.log('Schema验证错误:', ajv.errors);
    }
});

这样修改后,再用你提供的错误响应测试,AJV就会正确识别NestedAttribute类型不匹配的问题。

内容的提问来源于stack exchange,提问作者cin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 06:50:27