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

如何使用JSON Schema实现跨对象属性名的匹配校验

JSON Schema 完全支持这类跨字段属性名匹配校验,VS Code 内置的JSON校验引擎默认兼容所需规则,无需额外插件,对你现有Schema做少量修改即可实现需求。

具体修改步骤

  1. 指定Schema版本
    在你的Schema根节点添加$schema字段,声明使用2019-09及以上版本规范,避免校验器默认用旧版解析导致规则失效:

    "$schema": "https://json-schema.org/draft/2020-12/schema",
    
  2. 抽离统一的角色标签规则
    在根节点添加$defs字段定义角色标签的通用格式规则,同时修正你原有Schema里的规则位置错误:你之前把minItems、uniqueItems这类约束数组本身的规则写在了items(数组元素约束)里,这部分规则不会生效,需要挪到数组定义的同级位置。
    先添加通用标签定义:

    "$defs": {
      "characterTag": {
        "type": "string",
        "title": "Character tag",
        "description": "Short identifier for a character",
        "pattern": "^[a-z0-9_]+$"
      }
    }
    

    再修改characters字段的配置,给属性名增加格式约束:

    "characters": {
      "title": "Character collection",
      "description": "A collection of the available characters.",
      "type": "object",
      "propertyNames": { "$ref": "#/$defs/characterTag" },
      "additionalProperties":{
        "title": "Character entry",
        "type": "object",
        "properties": {
          "name": {
            "title": "Character name",
            "description": "Display name of the character.",
            "type": "string"
          },
          "hex_colour": {
            "title": "Character colour",
            "description": "Hex colour code in 0x000000 format.",
            "type": "string"
          },
          "image_url": {
            "title": "Character image",
            "description": "Public URL to the character's avatar.",
            "type": "string"
          }
        },
        "required": ["name"],
        "additionalProperties": false
      }
    }
    
  3. 给quotes对象增加属性名约束
    修改reactions数组项内quotes字段的规则,通过propertyNames+$data引用,强制要求quotes下的所有属性名必须是characters对象中已经定义过的角色标签,同时修正数组规则的位置:

    "quotes": {
      "title": "Quotes collection",
      "description": "Character mapped quote list for the reaction.",
      "type": "object",
      "propertyNames": {
        "$data": "/characters/propertyNames"
      },
      "additionalProperties":{
        "title":"Character quote list",
        "type": "array",
        "minItems": 1,
        "uniqueItems": true,
        "items": {
          "title": "Quote",
          "description": "Reply content sent when the keyword is triggered.",
          "type": "string"
        }
      }
    }
    

    顺便把同层级的keywords字段的规则位置也修正:

    "keywords": {
      "title": "Keyword list",
      "description": "List of trigger keywords for this reaction.",
      "type": "array",
      "minItems": 1,
      "uniqueItems": true,
      "items": {
        "title": "Keyword",
        "type": "string"
      }
    }
    

校验效果

修改完成后,只要你在quotes里填写了未在characters中定义的角色标签,VS Code会直接标红提示属性名非法;Python端如果用jsonschema 4.0+版本做运行时校验,默认支持该规则,不需要额外配置。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.01 04:55:19