Way to declare forbidden property

3,475 views
Skip to first unread message

jan.j...@gmail.com

unread,
Jan 29, 2014, 12:49:14 PM1/29/14
to json-...@googlegroups.com
Hi all,

what is the best way how to declare a forbidden property? The situation is that I have a JSON object, it has a set of properties very well specified in the schema and then it can have other additional properties. However, I'd like to restrict names of those additional properties so they don't interfere with couple of "reserved keywords" - therefore I am looking for a way how to mark some properties as "forbidden".

I came up with only one solution so far - I could define those keywords as optional properties with constraints which can be never satisfied. However, I consider it quite hackish :-/ Thanks for any suggestions!

Regards,
Honza

Geraint

unread,
Jan 29, 2014, 2:18:30 PM1/29/14
to json-...@googlegroups.com
I can see a couple of ways to do this using "not":

You could either supply impossible requirements, e.g.
{
    "properties": {
        "keyword": {"not": {}}  /* not allowed to be anything */
    }
}
Or you could make it anti-required:
{
    "not": {
        "required": ["keyword"]  /* must fail this, so "keyword" must not be present */
    }
}

Of these two, the first seems cleanest to me.  Schema-processing tools should be able to translate {"not": {}} into "this property is banned" fairly simply.

Honza Javorek

unread,
Jan 29, 2014, 3:12:16 PM1/29/14
to json-...@googlegroups.com
Great! Your solutions look very clean. Hopefully Python's jsonschema will get this right. I totally forgot JSON Schema has the concept of "not". Anyway, I would surely spend quite a while by formulating the right (and also the cleanest) way to write this.

Thank you very much.
Honza


--
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/groups/opt_out.

Eric G

unread,
Jan 29, 2014, 6:05:59 PM1/29/14
to json-...@googlegroups.com
Clever. I think the test suite should add a case for this use of not - when I have time I'll open a pull req.

Honza Javorek

unread,
Jan 30, 2014, 1:09:23 PM1/30/14
to json-...@googlegroups.com
Well, I tried the solutions with Python's jsonschema ( https://pypi.python.org/pypi/jsonschema ) and it could not understand the first way of declaring forbidden property. It did not even validate the schema. The second way works well.

Honza


On Thu, Jan 30, 2014 at 12:05 AM, Eric G <eric...@gmail.com> wrote:
Clever. I think the test suite should add a case for this use of not - when I have time I'll open a pull req.

Julian Berman

unread,
Jan 30, 2014, 1:47:28 PM1/30/14
to json-...@googlegroups.com
Both should work.

>>> jsonschema.Draft4Validator.check_schema({"properties" : {"keyword" : {"not" : {}}}})
>>> jsonschema.Draft4Validator({"properties" : {"keyword" : {"not" : {}}}}).validate({"keyword" : 12})
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/jsonschema/validators.py", line 117, in validate
    raise error
ValidationError: {} is not allowed for 12

Failed validating 'not' in schema['properties']['keyword']:
    {'not': {}}

On instance['keyword']:
    12

Honza Javorek

unread,
Jan 31, 2014, 10:08:23 AM1/31/14
to json-...@googlegroups.com
Julian, you're right, I just tried the same in my ipython console. I think I had to have typos in my schema which I overlooked, so I tried the other way instead of debugging and the other way worked for me for first time. I am glad you checked it so possible future readers of this thread will now know both ways are okay.

By the way, jsonschema library is awesome. I love iter_errors - this way I can easily validate incoming data in my API and return *all* the errors to API client in my 400 response.

Thank you!
Honza

simo...@gmail.com

unread,
Sep 5, 2016, 7:06:48 PM9/5/16
to JSON Schema, jan.j...@gmail.com


json-editor seems to fail on the first solution (unexpected behavior) 
Reply all
Reply to author
Forward
0 new messages