subtyping base types

630 views
Skip to first unread message

David B.

unread,
Jan 17, 2011, 9:18:25 AM1/17/11
to JSON Schema
I can't find a way to define a sub-types that could be reused to
define more complex objects.
There is an "extends" keyword that seems to be a feature for schema
inheritence but I would to declare my subtypes and their associated
objects in the same schema.
I think that adding a "subtypes" keyword could be an elegant way to
extend base types :

{
"description": "A schema with subtypes",
"subtypes": {
"person": {
"description": "A person subtype",
"type": "object",
"properties": { "name": { "type": "string" }
}
},
"adult": {
"description": "An adult subtype",
"type": "person",
"properties": { "age": { "minimum": 21 }
}
}
},
"properties": {
"adult_population": {
"type": "array",
"items": [ "adult" ]
}
},
"type": "object"
}

Gary Court

unread,
Jan 17, 2011, 1:28:22 PM1/17/11
to json-...@googlegroups.com
The "type" attribute can accepts schemas, which effectively allow you
to define subtypes. You can then use JSON Referencing to reuse a
schema, like so:

{
"id" : "person",


"description": "A person subtype",
"type": "object",
"properties": { "name": { "type": "string" }
}

{
"id" : "adult",


"description": "An adult subtype",

"type": {"$ref" : "person"},
"properties": { "age": { "minimum": 21 }
}
}

-Gary

> --
> You received this message because you are subscribed to the Google Groups "JSON Schema" group.
> To post to this group, send email to json-...@googlegroups.com.
> To unsubscribe from this group, send email to json-schema...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/json-schema?hl=en.
>
>

David B.

unread,
Jan 19, 2011, 4:40:15 AM1/19/11
to JSON Schema
Thank you for the tip Gary.

However following this solution I have to define two separate
documents.
I would like to do the same thing inside a unique schema as I can do
in a XSD for XML documents.

Is there a way to achieve this ?

-David

Gary Court

unread,
Jan 19, 2011, 5:15:41 PM1/19/11
to json-...@googlegroups.com
Yes, because of the "id" attribute, these schemas can both be embedded
into the same JSON Schema document without modifying the references.

-Gary

David B.

unread,
Jan 21, 2011, 10:11:55 AM1/21/11
to JSON Schema
Could you please give an example of this ?

-David

On 19 jan, 23:15, Gary Court <gary.co...@gmail.com> wrote:
> Yes, because of the "id" attribute, these schemas can both be embedded
> into the same JSON Schema document without modifying the references.
>
> -Gary
>

Gary Court

unread,
Jan 21, 2011, 11:33:30 AM1/21/11
to json-...@googlegroups.com
Any place you would use a reference, you can put the schema there. So:

{
"id" : "adult",
"description": "An adult subtype",
"type": {
"id" : "person",
"description": "A person subtype",
"type": "object",
"properties": { "name": { "type": "string" }

},
"properties": { "age": { "minimum": 21 }
}
}

-Gary

David B.

unread,
Jan 21, 2011, 12:37:15 PM1/21/11
to JSON Schema
Ok but what if I have to use a ref to the person schema multiple times
in the document ?
Does it imply I have to copy/paste the person schema multiple times in
the document ?
I would like to have it in the document as a reusable piece.

A solution I'm thinking about could be to have an option to hide some
elements of the schema which correspond to abstract schema that could
then be reused as ref in the document.

-David

On 21 jan, 17:33, Gary Court <gary.co...@gmail.com> wrote:
> Any place you would use a reference, you can put the schema there. So:
>
> {
>  "id" : "adult",
>  "description": "An adult subtype",
>  "type": {
>    "id" : "person",
>    "description": "A person subtype",
>    "type": "object",
>    "properties": {  "name": { "type": "string" }
>   },
>  "properties": { "age": {  "minimum": 21  }
>  }
>
> }
>
> -Gary
>

Gary Court

unread,
Jan 21, 2011, 11:57:31 PM1/21/11
to json-...@googlegroups.com
On Fri, Jan 21, 2011 at 10:37 AM, David B. <dbou...@gmail.com> wrote:
> Ok but what if I have to use a ref to the person schema multiple times
> in the document ?
> Does it imply I have to copy/paste the person schema multiple times in
> the document ?

No, when you need to use the same schema again, you can just use the
reference. (Although, there is nothing stopping you from including it
again if you wanted) A schema has to be defined at least once
somewhere for it to be used as a reference later. Example:

{


"id" : "adult",
"description": "An adult subtype",
"type": {
"id" : "person",
"description": "A person subtype",
"type": "object",
"properties": { "name": { "type": "string" } }
},
"properties": {

"age": { "minimum": 21 },
"son" : { "$ref" : "person" },
"daughter" : { "$ref" : "person" }
}
}

chuc...@gmail.com

unread,
Feb 8, 2011, 4:00:06 PM2/8/11
to json-...@googlegroups.com
Wouldn't you want to use "extends" instead of embedding person?  Using your example:

{
 "id":"person",
 "type":"object",
 "properties":{
   "name":{"type":"string"},
   "age":{"type":"integer"}
  }
}

{
 "id":"adult",
 "extends":{"$ref":"person"},
 "properties":{
   "age":{"type":"integer", "minimum":21},

as1234

unread,
Mar 11, 2011, 6:28:27 AM3/11/11
to JSON Schema
I can't find in v3 of the spec that "type" accepts a schema, unless
you want to build a union type.

And if, what would then be the semantic difference to "extends"?

Best,
Andreas

On Jan 17, 7:28 pm, Gary Court <gary.co...@gmail.com> wrote:
> The "type" attribute can accepts schemas, which effectively allow you
> to define subtypes. You can then use JSON Referencing to reuse a
> schema, like so:
>
> {
>   "id" : "person",
>   "description": "A person subtype",
>   "type": "object",
>   "properties": {  "name": { "type": "string" }
>
> }
>
> {
>   "id" : "adult",
>   "description": "An adult subtype",
>   "type": {"$ref" : "person"},
>   "properties": { "age": {  "minimum": 21  }
>   }
>
> }
>
> -Gary
>

Kenny Hoxworth

unread,
Mar 11, 2011, 8:25:51 PM3/11/11
to json-...@googlegroups.com
That's a good point, really; the spec does state "this attribute defines what the primitive type or the schema of the instance MUST be in order to validate", but the further description of the attribute seems to indicate that the attribute must either be one of the simple types listed or a union type. This should probably be clarified.

The json-schema Ruby validator that I've worked on assumes that the 'type' attribute adheres to the limits I stated above; if schemas are to be accepted as value of the 'type' attribute, it should be clearly enumerated with the other possible values.

Kenny Hoxworth

Gary Court

unread,
Mar 14, 2011, 11:58:18 AM3/14/11
to json-...@googlegroups.com, Kenny Hoxworth
You're right, my bad.

-Gary

Iain Duncan

unread,
May 11, 2011, 12:01:43 PM5/11/11
to json-...@googlegroups.com
Can you do the same trick of embedding the type definition into the extends property rather than using a $ref? so for example is this valid?:

{
 "id":"adult",
 "extends":{

   "id":"person",
   "type":"object",
   "properties":{
     "name":{"type":"string"},
     "age":{"type":"integer"}
    }
  },

Dean Landolt

unread,
May 11, 2011, 1:05:44 PM5/11/11
to json-...@googlegroups.com
I don't know the answer but I am curious: what's the use case for this? 

Gary Court

unread,
May 11, 2011, 1:55:26 PM5/11/11
to json-...@googlegroups.com
Yes, you can use $ref and schemas interchangeably, as references are
technically schemas.

-Gary

Iain Duncan

unread,
May 12, 2011, 4:37:32 AM5/12/11
to json-...@googlegroups.com
Hi Dean,

It was for the same reason as David had for the original post about types.  I want to have everything in a single schema file but still want to have some hierarchy between the different types.

Cheers,

Iain

Dean Landolt

unread,
May 16, 2011, 1:28:06 PM5/16/11
to json-...@googlegroups.com
On Thu, May 12, 2011 at 4:37 AM, Iain Duncan <iain.d...@gmail.com> wrote:
Hi Dean,

It was for the same reason as David had for the original post about types.  I want to have everything in a single schema file but still want to have some hierarchy between the different types.

Sure, but the reason I ask is that at this point what do you gain by using "extends"? If you're embedding the type definition you're not getting any reusability, so why not just inline it? The only thing I can think of is as a small convenience for testing. Is there something I'm missing?

Iain Duncan

unread,
May 17, 2011, 6:23:24 AM5/17/11
to json-...@googlegroups.com

If you're embedding the type definition you're not getting any reusability, so why not just inline it?

You can still get a bit of re-use within the same file so for example:

{
  "name" : "Adult",
  "extends": {
    "id" : "person",
    ...
  },
  "properties" : {
    "children" : {
      "type": "array",
      "items" : {
        "$ref" : "person"  // Re-use the person object that we are extending above
      }
    }
  }
}

I must admit though, having done this all in one file I didn't actually like it as you can end up with lots of nesting and it is far neater in my case at least to separate everything out!  Its nice to know that it is possible though, especially for small schemas or when writing a new one and just want to try a few different styles out.
Reply all
Reply to author
Forward
0 new messages