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

AJV条件/动态验证失效问题求助

AJV条件验证配置问题排查

问题描述

我搞不懂AJV的条件验证机制,试着用if-else做字段验证,但不确定配置对不对。我的验证JSON Schema如下:

{
    "$schema": "summaryAttributes#",
    "$id": "summaryAttributes",
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        },
        "processing": {
            "type": "object",
            "properties": {
                "full": { "type": "string" }
            },
            "required": [ "full" ]
        },
        "if": {
            "properties": {
                "name": "summary" 
            }
        },
        "then": {
            "properties": {
                "analysis": {
                    "type": "object",
                    "properties": {
                        "summary": { "type": "string" }
                    },
                    "required": [ "summary" ]
                }
            }
        },
        "else": {
            "properties": {
                "analysis": {
                    "type": "object",
                    "properties": {
                        "fileDescription": { "type": "string" }
                    },
                    "required": [ "fileDescription" ]
                }
            }
        },
        "additionalProperties": false,
        "required": [ "name", "processing", "analysis" ]
    }
}

有一段我认为应该验证不通过,但实际通过的数据:

{
    "name": "summary",
    "processing": {
        "full": "Some random text"
    },
    "analysis": {
        "summary": ""
    }
}

问题原因及修复方案

1. if条件配置错误

你的if块里,properties.name直接写了字符串"summary",不符合JSON Schema规范。要匹配name字段等于特定值,需用const关键字定义schema,同时建议添加required确保name字段存在时才触发条件判断:

"if": {
    "properties": {
        "name": { "const": "summary" }
    },
    "required": ["name"]
}

2. summary字段未限制空字符串

当前then块里的summary仅设置了type: "string",而空字符串""属于合法字符串类型,所以验证会通过。如果要禁止空值,需添加minLength: 1约束:

"then": {
    "properties": {
        "analysis": {
            "type": "object",
            "properties": {
                "summary": { 
                    "type": "string",
                    "minLength": 1
                }
            },
            "required": [ "summary" ]
        }
    }
}

修复后的完整Schema

{
    "$schema": "summaryAttributes#",
    "$id": "summaryAttributes",
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        },
        "processing": {
            "type": "object",
            "properties": {
                "full": { "type": "string" }
            },
            "required": [ "full" ]
        },
        "if": {
            "properties": {
                "name": { "const": "summary" }
            },
            "required": ["name"]
        },
        "then": {
            "properties": {
                "analysis": {
                    "type": "object",
                    "properties": {
                        "summary": { 
                            "type": "string",
                            "minLength": 1
                        }
                    },
                    "required": [ "summary" ]
                }
            }
        },
        "else": {
            "properties": {
                "analysis": {
                    "type": "object",
                    "properties": {
                        "fileDescription": { "type": "string" }
                    },
                    "required": [ "fileDescription" ]
                }
            }
        },
        "additionalProperties": false,
        "required": [ "name", "processing", "analysis" ]
    }
}

修改后,你提供的测试数据因summary为空字符串,会触发验证失败,符合预期。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 12:07:23