You need to enable JavaScript to run this app. 基于只读独立枚举JSON Schema编写带联动规则的严格校验方法
优惠活动
大模型
产品
解决方案
定价
更多

基于只读独立枚举JSON Schema编写带联动规则的严格校验方法

只读枚举Schema的上层扩展实现方案

先修正初步框架的核心结构错误

你当前搭建的框架存在两处会导致校验完全失效的问题:

  • 引用路径错位:原只读Schema的根对象是直接定义Anilmals、Breeds两个平级属性的,你之前写的ReferenceToExternalSchema: { $ref: "masterh-husbandry.json#/properties" },会把这两个字段嵌套到ReferenceToExternalSchema的子层级下,完全偏离原Schema要求的结构
  • 约束层级错位:没有把原Schema的基础校验规则直接绑定到根对象上,后续要实现的跨字段联动,也无法关联到实际存在的Anilmals、Breeds字段

跨字段联动的实现逻辑

实现“Anilmals为Cat时Breeds仅能取Cheshire”这类规则,不需要修改原只读Schema的任何内容,只需要做两层约束叠加:

  1. 把原只读Schema的全部规则通过allOf引入上层Schema,确保所有校验不突破原Schema定义的范围(包括字符串长度、枚举值范围、字段类型要求)
  2. 叠加条件校验规则:利用draft-07原生支持的if/then语法,对不同的Anilmals取值,限定对应的Breeds合法范围,所有限定值全部在原Schema定义的枚举集合内,不会违反原规则要求

可直接落地的完整上层Schema

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "additionalProperties": false,
    "allOf": [
        { "$ref": "masterh-husbandry.json#" },
        {
            "if": {
                "properties": {
                    "Anilmals": { "const": "Cat" }
                },
                "required": ["Anilmals"]
            },
            "then": {
                "properties": {
                    "Breeds": { "const": "Cheshire" }
                },
                "required": ["Breeds"]
            }
        },
        {
            "if": {
                "properties": {
                    "Anilmals": { "const": "Dog" }
                },
                "required": ["Anilmals"]
            },
            "then": {
                "properties": {
                    "Breeds": { "const": "Poodle" }
                },
                "required": ["Breeds"]
            }
        },
        {
            "if": {
                "properties": {
                    "Anilmals": { "const": "Bear" }
                },
                "required": ["Anilmals"]
            },
            "then": {
                "properties": {
                    "Breeds": { "const": "Polar" }
                },
                "required": ["Breeds"]
            }
        },
        {
            "if": {
                "properties": {
                    "Anilmals": { "const": "Human" }
                },
                "required": ["Anilmals"]
            },
            "then": {
                "properties": {
                    "Breeds": { "const": "Trump" }
                },
                "required": ["Breeds"]
            }
        }
    ],
    "properties": {
        "no-write": {
            "type": "boolean",
            "default": false
        }
    }
}

落地注意事项

  • 不要随意修正原Schema里的历史拼写问题(比如把Anilmals调整为Animals),否则会和原Schema的字段定义匹配失败,直接导致校验规则失效
  • 所有跨字段限定的Breeds值,必须全部落在原Schema定义的枚举集合范围内,不要引入原枚举外的新取值,否则会违背原只读Schema的约束要求
  • 相比用oneOf枚举所有合法配对组合的实现方式,if/then的逻辑可读性更高,后期调整配对规则时不需要全量重构枚举列表,维护成本更低

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.20 13:45:04