"All other properties in a "$ref" object MUST be ignored." - so can't "subclass" a schema?

209 views
Skip to first unread message

Vladimir Dyuzhev

unread,
Mar 18, 2019, 5:40:47 AM3/18/19
to JSON Schema
Hello, folks. Newbie here.

Draft 7 "8.3 Schema references with $ref" says that if a schema object has $ref, all other properties MUST be ignored.

That made me pause. I imagine it can be useful to create a base schema #abstract-base and then override some of its properties, e.g. add numerical restrictions:

"wholesale": {
  "$ref": "#/definitions/abstract-base",
  "minimum": 20
},

"retail": {
  "$ref": "#/definitions/abstract-base",
  "minimum": 1,
  "exclusiveMaximum": 20,
}

Where can I learn the reasoning for the strict $ref?

Also, since above subclassing is not valid, can the same effect be achieved in some other way (save for copy-pasting abstract-base properties into both "subclassed" objects)?

Thanks!


Ben Hutton

unread,
Mar 18, 2019, 5:45:13 AM3/18/19
to Vladimir Dyuzhev, JSON Schema

Hi Vladimir,

 

No one currently working on the project is really sure WHY.

We have changed this for draft-8!

 

See https://github.com/json-schema-org/json-schema-spec/pull/628

 

 

Cheers

 

Ben

(JSON Schema team)

-- The Wellcome Sanger Institute is operated by Genome Research Limited, a charity registered in England with number 1021457 and a company registered in England with number 2742969, whose registered office is 215 Euston Road, London, NW1 2BE.

Austin William Wright

unread,
Mar 23, 2019, 8:26:45 PM3/23/19
to JSON Schema
The reason is for historical reasons: objects with a $ref property aren't considered schemas, but instead they're a "JSON Reference", which used to be a separate, re-usable specification. This wasn't a good idea for several reasons, so in a removed the reference, but had to keep the same behavior: Since other properties in these objects weren't considered schemas, they're ignored.

As of recently, JSON Schema no longer has this restriction, however for compatibility, I would put $ref references inside an allOf statement:

"wholesale": {
 "allOf": [ { "$ref": "#/definitions/abstract-base" } ],
 "minimum": 20
},

One more note, when you add keywords like this, what you're doing is creating a subset, which sometimes isn't quite the same thing as a subclass, depending on how you're going about it.

Henry Andrews

unread,
Mar 23, 2019, 9:00:46 PM3/23/19
to JSON Schema
One more note, when you add keywords like this, what you're doing is creating a subset, which sometimes isn't quite the same thing as a subclass, depending on how you're going about it.

Yes, this is a key point.  JSON Schema is a constraint system, not a definition system.  In a definition system, the empty object does nothing.  In a constraint system, the empty schema allows everything.

thanks,
-henry


--
You received this message because you are subscribed to the Google Groups "JSON Schema" group.
To unsubscribe from this group and stop receiving emails from it, send an email to json-schema...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages