schema icon indicating copy to clipboard operation
schema copied to clipboard

Combining JSON API example with ignore_extra_keys=True fails

Open marenaud opened this issue 9 years ago • 5 comments
trafficstars

While attempting to follow the JSON API example, I stumbled upon a use case that I think should work but doesn't:

schema = Schema(And(Use(json.loads), {'foo': basestring}), ignore_extra_keys=True)
woops = json.dumps({'foo': 'hello', 'bar': 'asdf'})
schema.validate(woops)
>> SchemaError: Wrong keys u'bar' in {u'foo': u'hello', u'bar': u'asdf'}

The regular example works fine:

schema2 = Schema({'foo': basestring}, ignore_extra_keys=True)
schema2.validate({'foo': 'hello', 'bar': 'asdf'})
>> {'foo': 'hello'}

marenaud avatar May 18 '16 18:05 marenaud

Try the ignore_extra_keys parameter in the And, does it still fail?

skorokithakis avatar May 18 '16 18:05 skorokithakis

Doesn't work:

schema = Schema(And(Use(json.loads), {'foo': basestring}, ignore_extra_keys=True))
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-34-c260859843e0> in <module>()
----> 1 schema = Schema(And(Use(json.loads), {'foo': basestring}, ignore_extra_keys=True))

/usr/local/lib/python2.7/dist-packages/schema.pyc in __init__(self, *args, **kw)
     30     def __init__(self, *args, **kw):
     31         self._args = args
---> 32         assert list(kw) in (['error'], [])
     33         self._error = kw.get('error')
     34 

AssertionError:

marenaud avatar May 18 '16 18:05 marenaud

Hmm, I'll have a look, thanks.

skorokithakis avatar May 18 '16 19:05 skorokithakis

It's probably not too urgent since it's so easy to just json.loads the data and validating that instead.

marenaud avatar May 18 '16 19:05 marenaud

Sure, but it should still be fixed.

skorokithakis avatar May 18 '16 19:05 skorokithakis