additionalProperties inheritance

203 views
Skip to first unread message

Lu Ai

unread,
Jan 29, 2014, 3:53:40 PM1/29/14
to json-...@googlegroups.com
My json schema is long and I don't want to allow additional properties other than what I specify in my schema. In the example below, at the root, I specify "additionalProperties": false. Is it inheritable by other json objects in the schema? If I don't want to allow additional properties for "foo1" and "foo2", do I have to repeat "additionalProperties": false with each occurrence of "properties"? Is there a way to say the root "additionalProperties": false is inheritable by other json objects in the schema?

{
    "additionalProperties": false,
    "properties": {
        "foo1": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "fooa": {"type": "string"},
                    "foob": {"type": "string"},
                    "fooc": {"type": "string"}
                }
            }
        },
        "foo2": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "fooe": {"type": "string"},
                    "foof": {"type": "string"},
                    "foog": {"type": "string"}
                }
            }
        }
    }
}

Geraint

unread,
Jan 30, 2014, 5:06:47 AM1/30/14
to json-...@googlegroups.com
The schema you have written there bans all properties apart from "foo1" and "foo2".

If you extend this schema, then the new schema will still ban all properties apart from "foo1" or "foo2", because it inherits all the constraints of the other schema.

If you want to be able to define new properties (other than "foo1" and "foo2", then you cannot use "additionalProperties" in your original schema.  If you want to just ban unknown properties during validation, then you might want to use a validator that supports "ban unknown properties" mode.

Lu

unread,
Jan 30, 2014, 10:42:00 AM1/30/14
to json-...@googlegroups.com
>>If you want to be able to define new properties (other than "foo1" and "foo2", then you cannot use "additionalProperties" in your original schema.  If you want to just ban unknown properties during validation, then you might want to use a validator that supports "ban unknown properties" mode.

For now, we only want to allow properties listed in our json schema. In the future, we might need to add new properties. The way we vision to achieve this, is to version our json schema and use "additionalProperties": false in each version. Is this a misunderstanding/misuse of "additionalProperties"? Whats' the use case of "additionalProperties":false then?

Thanks for pointing out the proposed "ban unknown properties" mode with json validators. It doesn't look like many validators support it. One of the main drives for us to adopt json schema is that we want to be able to use the json schema to validate incoming json documents. Without the use of "additionalProperties":false, we can't even catch a typo in property names. I believe quite a few json schema implementers have the same understanding likewise.

Thanks,

Lu     


--
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/WZ9u80sXMKk/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,
Jan 31, 2014, 4:36:54 AM1/31/14
to json-...@googlegroups.com
On Thursday, January 30, 2014 3:42:00 PM UTC, Lu Ai wrote:
For now, we only want to allow properties listed in our json schema. In the future, we might need to add new properties. The way we vision to achieve this, is to version our json schema and use "additionalProperties": false in each version. Is this a misunderstanding/misuse of "additionalProperties"? Whats' the use case of "additionalProperties":false then?

That sounds like it will work.  If you are adding new properties to the existing schema, then you will get the behaviour you want.

I apologise for the confusion - I misunderstood what you meant by "inheritance".  I thought you were talking about extending schemas using "allOf".

However, your actual question was about whether "additionalProperties" cascades down into sub-schemas.  The answer is "no" - each schema/sub-schema is considered separately, and you need to specify "additionalProperties" in the sub-schemas as well.

Geraint

Lu

unread,
Feb 2, 2014, 8:15:50 PM2/2/14
to json-...@googlegroups.com
Thanks for the explanation. My schema contains many sub-schemas. I have to use "additionalProperties:false" for all of them, otherwise I can't even catch a typo in the incoming json documents, like I said. On the other hand, "additionalProperties:false" makes it difficult for me to reuse some of the sub-schemas for other purposes. I think I really like the "ban unknown properties" mode v5 proposal. If I have a validator that supports this mode, I really can't think of a use case for "additionalProperties:false". Also, does anybody actually version their json schema?  




--
Reply all
Reply to author
Forward
0 new messages