Formatter

362 views
Skip to first unread message

Paul Kavanagh

unread,
Apr 18, 2013, 4:36:34 PM4/18/13
to jsons...@googlegroups.com
I'm not sure how to invoke the format checker.

v = Draft4Validator(test_schema, format_checker=FormatChecker()) #works but doesn't check anything
v = Draft4Validator(test_schema, format_checker=FormatChecker(formats=("email")) #blows up with a KeyError on e

Does this work currently?

I've got the source of course but am a bit hazy on Python class invocation.
I'd be happy to contribute some format checkers (phone, email, lat/long, gender, repetition date/rule) which I need, if I was able to get this working.

Julian Berman

unread,
Apr 18, 2013, 4:41:46 PM4/18/13
to jsons...@googlegroups.com
Hey there.

On Thursday, April 18, 2013 4:36:34 PM UTC-4, Paul Kavanagh wrote:
I'm not sure how to invoke the format checker.

v = Draft4Validator(test_schema, format_checker=FormatChecker()) #works but doesn't check anything

That looks right. FormatChecker() by itself constructs a thing that knows about all the formats jsonschema knows about (i.e. ones from draft 3 along with ones from draft4). Passing something else in there restricts the format checkers it knows about to just the ones you pass in, so FormatChecker(["email"]) (note that it has to be an *iterable* (list) of names, not just a string), will just know about the "email" format.

It should work though. E.g. I get:

>>> import jsonschema; jsonschema.validate("cow", {"format" : "ipv4"}, format_checker=jsonschema.FormatChecker())
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/jsonschema.py", line 1301, in validate
    cls(schema, *args, **kwargs).validate(instance)
  File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/jsonschema.py", line 227, in validate
    raise error
ValidationError: 'cow' is not a 'ipv4'
 

Can you post your instance and schema or a reproducing example of something you expect to work but doesn't?

Julian

Paul Kavanagh

unread,
Apr 18, 2013, 5:12:31 PM4/18/13
to jsons...@googlegroups.com
Here is the test program and the schema it is using. I think it should be getting errors for the email format and the date-time of dtstart.
Phone is bad as well but I don't think phone actually has a formatter.
This is a bit large, I will try to boil it down.


On Thursday, April 18, 2013 4:36:34 PM UTC-4, Paul Kavanagh wrote:
js_test.py
test_schema.json

Paul Kavanagh

unread,
Apr 18, 2013, 5:20:50 PM4/18/13
to jsons...@googlegroups.com
Ran a simple test and it worked (attached). 

'XXX' is not a 'email'

I need to do a little more work. Maybe the format test only works on the top level, not when embedded in objects.

On Thursday, April 18, 2013 4:36:34 PM UTC-4, Paul Kavanagh wrote:
test1.py

Chase Sterling

unread,
Apr 18, 2013, 5:30:56 PM4/18/13
to Paul Kavanagh, jsons...@googlegroups.com
A couple things. You need to have the isodate module installed to get the date-time checker, with that your test did come up with the error. Also, your email part of the schema doesn't exactly describe the data you are giving, replacing it with this:

"email": {
    "type": "object",
    "additionalProperties": {
        "type": "string",
        "format": "email"
    }
}

The email format checker fails for your instance. Might not be the exact schema you want though.


--
You received this message because you are subscribed to the Google Groups "jsonschema - An implementation of JSON Schema for Python" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema+...@googlegroups.com.
To post to this group, send email to jsons...@googlegroups.com.
 
 

Lance Bragstad

unread,
Jun 4, 2014, 2:50:25 PM6/4/14
to jsons...@googlegroups.com, pkav...@noodle.org
Hello all,

I had a quick question on format checking URIs specifically. When I create my validator object, I have an instance of FormatChecker() passed in as a keyword argument [1]. According to the latest jsonschema documentation [2], I would need rfc3987 [3] in order to use the FormatChecker to validate URIs. I grabbed rfc3987 from PyPI and I seem to be able to use that fine [4]. However, when I specify 'uri' in an iterable and pass that to FormatChecker as a keyword argument to formats, I get the same KeyError as mentioned above.

When I don't specify format in FormatChecker(), attach a debugger, and step into my validate call [5], I don't see 'uri' as an entry in the checkers dictionary [6]. Shouldn't I be able to do this give the logic in  _format.py [7]?

I could be missing something here, just staring to take a look into jsonschema, and following documentation. Thank you for your time and let me know if there is anything I can do.

[1] http://paste.openstack.org/show/82823/
[2] http://python-jsonschema.readthedocs.org/en/latest/validate/#validating-formats
[3] https://pypi.python.org/pypi/rfc3987/
[4] http://paste.openstack.org/show/82821/
[5] https://github.com/Julian/jsonschema/blob/master/jsonschema/validators.py#L115-L117
[6] http://paste.openstack.org/show/82822/
[7] https://github.com/Julian/jsonschema/blob/master/jsonschema/_format.py#L160-L169

Julian Berman

unread,
Jun 5, 2014, 9:44:30 PM6/5/14
to jsons...@googlegroups.com, pkav...@noodle.org
Hey. It would sound like possibly that rfc3987 was installed to a different Python than the one you're using to validate, or that your environment is otherwise different when running your validation script -- can you possibly try stepping into importing jsonschema and seeing the ImportError fail for rfc3987 (or report back that it doesn't :) you can find it at https://github.com/Julian/jsonschema/blob/master/jsonschema/_format.py#L161

-Julian
Reply all
Reply to author
Forward
0 new messages