I'm learning JSON Schema for one of the open-source projects. Have a question concerning combining an already defined schema with some additional keywords.
Assume I've defined some schema which is available via the reference, for example #/definitions/positiveInteger
.
Now I would like to refer to that type and extend it with few other keywords. For instance, I'd like to add enum
or description
.
Is it allowed to do this:
{
"$ref" : "#/definitions/positiveInteger",
"description" : "This is positive integer with enums",
"enum" : [ 2, 4, 6, 8 ]
}
Or do I have to do this:
{
"allOf" : [ { "$ref" : "#/definitions/positiveInteger" } ],
"description" : "This is positive integer with enums",
"enum" : [ 2, 4, 6, 8 ]
}
I could not find the formal reference for that and not sure how validation tools would react.
Basically I have two question here:
enum
work in the first case?description
next to $ref
like in the first case?I'd actually prefer to inline $ref
s and not to include them in allOf
and kinds but not sure if this is legal. I couldn't even find a description of $ref
in the spec (just a couple of examples).
You have to use allOf
to extend a definition. Here is the relevant documentation.
Any members other than "$ref" in a JSON Reference object SHALL be ignored.
- https://datatracker.ietf.org/doc/html/draft-pbryan-zyp-json-ref-03#section-3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With