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

如何在JSON Schema中限制dependencies数组值与uuid属性不重复

解决方案

首先要修正原Schema里的拼写错误:字段名depedencies应为dependencies,否则约束不会生效。

实现约束的方法

JSON Schema Draft 04本身没有原生支持跨字段引用实例值的能力,但多数主流校验器(如Ajv)支持$data扩展关键字,可用来引用当前实例的其他字段值。借助这个扩展,我们可以实现dependencies数组元素不等于当前DLC的uuid的约束。

修改后的完整Schema

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "JSON schema for DLC Metadata",
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "description": { "type": "string" },
    "uuid": {
      "description": "The unique identifier for the DLC",
      "type": "string",
      "minLength": 36,
      "maxLength": 36,
      "pattern": "^([a-zA-Z0-9]{8})-([a-zA-Z0-9]{4})-([a-zA-Z0-9]{4})-([a-zA-Z0-9]{4})-([a-zA-Z0-9]{12})$"
    },
    "version": {
      "type": "array",
      "items": [
        { "type": "integer" },
        { "type": "integer" },
        { "type": "integer" }
      ]
    },
    "dependencies": {
      "type": "array",
      "items": {
        "description": "The uuid that this DLC depends on",
        "type": "string",
        "minLength": 36,
        "maxLength": 36,
        "pattern": "^([a-zA-Z0-9]{8})-([a-zA-Z0-9]{4})-([a-zA-Z0-9]{4})-([a-zA-Z0-9]{4})-([a-zA-Z0-9]{12})$",
        "not": {
          "enum": [{"$data": "/uuid"}]
        }
      },
      "uniqueItems": true
    }
  },
  "required": [
    "name",
    "uuid",
    "version"
  ]
}

关键修改说明

  • 修正了字段名拼写错误:depedencies → dependencies;
  • 在dependencies.items中添加not约束:通过$data引用当前实例的uuid值,确保数组中的每个元素都不能与该值相等。

严格遵循Draft 04标准的替代方案

如果你的校验器不支持$data扩展,原生Draft 04无法实现这种跨字段的实例值约束,此时需要在业务逻辑中额外校验:遍历dependencies数组,检查每个元素是否等于当前的uuid,若存在匹配项则拒绝该元数据。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 02:37:38