如何在 JsonSchema 中指定 Type 为另一个对象的映射

问题描述 投票:0回答:1

我想在 JsonSchema 中指定一个映射。 Java 的等价物应该是 Map

我发现在JsonSchema中,Map可以指定为:

...
{
  "type": "object",
  "additionalProperties": {
    "type": "string"
  }
}
...

地图相当于:

...
"$defs": {
  "SomeType": {
    // type, properties, etc
  },
  ...
}
...
{
  "type": "object",
  "additionalProperties": {
    "$ref": "#/$defs/SomeType"
  }
}
...

想知道这是否有效 JsonSchema (2019-09)

谢谢!

jsonschema
1个回答
0
投票

这绝对是有效的 JSON 架构,但对于 2019-09 或更高版本,您应该使用未评估的*关键字。


{
    "$schema": "https://json-schema.org/draft-2020-12/schema",
    "type": "object",
    "unevaluatedProperties": {
        "$ref": "#/$defs/some_type"
    },
    "$defs": {
        "some_type": {
            "type": object,
            "properties": {
              "name": {"type": "string"}
            }
        }
    }
}

这将是有效的

{"random": {"name": "test"}}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.