How to extend an existing JSON schema?

2,285 views
Skip to first unread message

Hao HU

unread,
Nov 12, 2013, 10:16:54 AM11/12/13
to json-...@googlegroups.com
Hi,

May I ask one naive question?

Suppose that there exists already a JSON schema. We have written some JSON codes on respecting this schema. Now we would like to extend this schema by adding some new properties. How would it be possible to extend the old schema in this way without breaking the support for old JSON codes?


Best regards,

Hao

Hao HU

unread,
Nov 12, 2013, 10:17:36 AM11/12/13
to json-...@googlegroups.com
Does there exist something like protobuf field number?

Regards,

Hao

Geraint

unread,
Nov 12, 2013, 9:00:32 PM11/12/13
to json-...@googlegroups.com
JSON data is generally tolerant of extra/unknown properties - they are usually just ignored.

Because of this, most schemas allow additional properties.  If this is the case, then you should be able to extend the schema using "allOf", and define some new properties:
{
    "allOf": [{"$ref": "/some/other/schema"}],
    "properties": {
        ...
    }
}

As long as the original schema did not specify "additionalProperties":false, then there should be no problem. :)

Geraint

Hao HU

unread,
Nov 13, 2013, 3:58:11 AM11/13/13
to json-...@googlegroups.com
Thanks a lot.
I heard that if we extend a schema with "additionalProperties":false specified, the new schema will not merge the properties of the parent schema into its own? Is that still true?
Or, If for example, I would like to overwrite a property of the parent schema. i.e delete one old property of the parent schema and then add a new one to take its place. How could it be possible in JSON schema? Or is there any alternatives to realize this?

Regards,

Hao


2013/11/13 Geraint <gerai...@gmail.com>

--
You received this message because you are subscribed to a topic in the Google Groups "JSON Schema" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/json-schema/hjemODGbW0g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to json-schema...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Geraint

unread,
Nov 13, 2013, 4:46:57 AM11/13/13
to json-...@googlegroups.com
That is correct - if schema A bans additional properties, then a schema B extending that cannot define new properties.

If you want to extend the schema later, then you must not specify "additionalProperties":false.

Instead, if you really need to have no unknown properties (while allowing extensions), you could consider validating using "Ban Unknown Properties" mode.  A validator operating in this mode will disallow any properties that it does not recognise.  This mode is optional, but some validators already support it - what platform/environment are you using?

Geraint

Hao HU

unread,
Nov 13, 2013, 5:20:54 AM11/13/13
to json-...@googlegroups.com
Thanks,

I am sorry. I made a mistake. Actually what I want to say is that, I heard that even when we enable the "additionalProperties":true; the inherited properties won't be merged into the properties of child schema? You could see this link: https://groups.google.com/forum/#!topic/json-schema/Ru-tCCUlDUU

Regards,

Hao

Geraint

unread,
Nov 13, 2013, 7:46:28 AM11/13/13
to json-...@googlegroups.com
Ah, I see!

Yes - "additionalProperties" refers only to the properties defined in that immediate schema.

This allows you to restrict/ban properties in the sub-schema.  So if Schema A has some optional properties, then Schema B is allowed to extend Schema A but ban those properties - this is completely valid, because the properties are optional, so all data following Schema B will still follow Schema A.

If "additionalProperties" used properties from extended/referenced schemas, then this wouldn't be possible (as well as making implementations much more complex).

Does that make sense?

Geraint
Reply all
Reply to author
Forward
0 new messages