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

JSON Schema多条件if-else验证失效问题求助

JSON Schema多条件if-else验证失效问题求助

各位大佬好,我最近在写JSON Schema的时候遇到了一个条件验证的问题,折腾半天没搞定,想请教下大家。

我的业务场景是这样的:我需要通过name和value的键值对往表格里添加元素,核心验证规则是:

  • 当元素的name字段值为'name'时,对应的value必须符合指定的正则表达式
  • 但有个例外:如果这条元素所属的tableName是'header',那不管name是不是'name',value都不需要遵循这个正则限制

我自己写了一部分Schema,结构大概是这样的:

{
    "properties": {
        "elements": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/arrayElement"
            },
            "minItems": 1
        }
    },
    "definitions": {
        "arrayElement": {
            "type": "object",
            "required": ["tableName"],
            "properties": {
                "tableName": { "type": "string" },
                "name": { "type": "string" },
                "value": { "type": "string" }
                // 其他字段省略
            }
            // 我在这里尝试加if/then/else逻辑,但没生效
        }
    }
}

我尝试用if/then/else来实现这个多条件判断,但要么是所有name='name'的情况都被正则限制了(包括tableName='header'的场景),要么是例外场景的验证完全没起作用。比如我试过这样写条件:

"if": {
    "allOf": [
        { "properties": { "name": { "const": "name" } } },
        { "properties": { "tableName": { "not": { "const": "header" } } } }
    ]
},
"then": {
    "properties": {
        "value": { "pattern": "^[A-Za-z0-9]+$" }
    }
}

但结果还是不对,tableName='header'且name='name'的元素,value还是会被正则校验。

有没有大佬能帮我看看哪里出问题了?或者正确的实现方式应该是怎样的?感谢大家!

备注:内容来源于stack exchange,提问作者Atin Singh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.15 14:14:51