Is it mandatory to use $defs for reused blocks #151
-
I have a bit of a concern regarding the use of the According to the description in Understanding JSON Shema "the What if I wanted to keep such data in another place? Is this considered bad design, or is it acceptable? I'll give two different situations, both of which validate correctly.
Is it necessary here to make a new {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"a": {"$ref": "#/$defs/TypeGroupOne/Foo"}
},
"required": ["a"],
"$defs": {
"TypeGroupOne": {
"Foo": { "type": "integer"},
"Bar": { "type": "string"}
},
"TypeGroupTwo": {}
}
}
This is mainly for use in auxiliary files - for example, we have File {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"TypeGroupOne": {
"Foo": { "type": "integer"},
"Bar": { "type": "string"}
},
"TypeGroupTwo": {}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The only problem with that approach is that you don't get meta-schema validation if put your schemas somewhere that the dialect doesn't recognize as a schema. Take this schema for example. {
"$defs": {
"foo": { "type": "invalid" },
"bar": {
"nested": { "type": "invalid" }
}
}
} Implementations are likely to recognize If you want to have nested definitions, you could use {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"a": {"$ref": "TypeGroupOne#/$defs/Foo"}
},
"required": ["a"],
"$defs": {
"TypeGroupOne": {
"$id": "TypeGroupOne",
"$defs": {
"Foo": { "type": "integer"},
"Bar": { "type": "string"}
}
},
"TypeGroupTwo": {
"$id": "TypeGroupTwo",
"$defs": {}
}
}
} Edit: Actually, you don't need to the |
Beta Was this translation helpful? Give feedback.
The only problem with that approach is that you don't get meta-schema validation if put your schemas somewhere that the dialect doesn't recognize as a schema. Take this schema for example.
Implementations are likely to recognize
#/$defs/foo/type
has an error, but not#/$defs/bar/nested/type
. Instead of telling you your schema has an error in the normal way, it will probably just blow up and you'll have to sift through the wreckage to figure out what went wrong.If you want to have nested definitions, you could use
$id
to create a separate context. It's a bit more verbose, but it…