schema
schema copied to clipboard
Combining JSON API example with ignore_extra_keys=True fails
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'}
Try the ignore_extra_keys parameter in the And, does it still fail?
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:
Hmm, I'll have a look, thanks.
It's probably not too urgent since it's so easy to just json.loads the data and validating that instead.
Sure, but it should still be fixed.