7

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:

  • Should the validation keyword like enum work in the first case?
  • Will be there any conflicts if I include a metadata keyword like description next to $ref like in the first case?

I'd actually prefer to inline $refs 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).

S.A.
  • 1,819
  • 1
  • 24
  • 39
lexicore
  • 42,748
  • 17
  • 132
  • 221

2 Answers2

8

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.

Community
  • 1
  • 1
Jason Desrosiers
  • 22,479
  • 5
  • 47
  • 53
1

From Draft 2019-09, $ref and other properties can co-exist together.

JSON Schema Spec

Although the spec seems a little unclear on what happens when the reference schema and the importing schema declare the same properties

Sprotty
  • 5,676
  • 3
  • 33
  • 52