带有自定义 属性 的 jsonschema

jsonschema with custom property

我想定义一个 JsonSchema,里面有一个 customProperty,这个 属性 遵循一些规则,所以为了验证这些,我需要定义一个 JsonSchema这将验证它。

到目前为止我已经设法正确描述它,但它只适用于第一级属性,我希望它是递归的...

根据我的理解,它应该可以工作,我可能犯了一个我看不到的错误,此时我不知道它是错误、不可能还是愚蠢...

我相信重新定义每种类型应该是可能的,但显然我不想这样做。

这里有一个例子 Json 我要验证

{
  "title": "TheObject",
  "type": "object",
  "properties": {
    "aString": {
      "type": "string",
      "myCustomProperty": {}
    },
    "anObjet": {
      "type": "object",
      "myCustomProperty": {},
      "properties": {
        "anotherObject": {
          "type": "object",
          "myCustomProperty": {}, //if this line is removed it still validates wich I don't want
          "properties": {}
        }
      }
    }
  }
}

这是我目前所做的Json架构:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "title": {"type": "string"},
    "type": {"type": "string","enum": ["object"]},
    "properties": {
      "type": "object",
      "patternProperties": {
        ".*": {
          "$ref": "#/definitions/Field"
        }
      }
    }
  },
  "definitions": {
    "Field": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string"
        },
        "myCustomProperty": {
          "$ref": "#/definitions/myCustomProperty"
        },
        "patternProperties": {
          "^(?!myCustomProperty).*": {
            "$ref": "#/definitions/Field"
          }
        }
      },
      "required": [
        "type",
        "myCustomProperty"
      ]
    },
    "myCustomProperty": {
        //Some rules
    }
  }
}

我找到了解决办法,毕竟离我想要的不远

在我对 "Field" 的定义中,我描述了一个定义对象的对象,但我缺少 "properties" 字段。我不得不在其中放置递归引用。

正确的 jsonSchema 如下:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "title": {
      "type": "string"
    },
    "type": {
      "type": "string",
      "enum": [
        "object"
      ]
    },
    "properties": {
      "type": "object",
      "patternProperties": {
        ".*": {
          "$ref": "#/definitions/Field"
        }
      }
    }
  },
  "definitions": {
    "Field": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string"
        },
        "myCustomProperty": {
          "$ref": "#/definitions/myCustomProperty"
        },
        "properties": {            <====================  here
          "type": "object",
          "patternProperties": {
            ".*": {
              "$ref": "#/definitions/Field"
            }
          }
        }
      },
      "required": [
        "type",
        "myCustomProperty"
      ]
    },
    "myCustomProperty": {/*rules*/}
  }
}

到目前为止它按预期工作,但如果有人有更优雅的建议请分享!