jsonschema

How to specify a map in JsonSchema where the Type is another object


I would like to specify a map in JsonSchema. The Java equivalant should be a Map<String, SomeType>.

I found that in JsonSchema a Map<String, String> can be specified as:

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

Would the Map<String, SomeType> equivalant be:

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

Wondering if that would be valid JsonSchema (2019-09)

Thanks!


Solution

  • This is definitely valid JSON Schema, but with 2019-09 or newer, you should use the unevaluated* keywords.

    
    {
        "$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"}
                }
            }
        }
    }
    

    This would be valid

    {"random": {"name": "test"}}