schema
schema copied to clipboard
Reference values in error messages.
It would be good if the user-provided values could be referenced in error messages:
schema = Schema( {'<file>': [And(os.path.isfile, error='File "{value}" does not exist')]
...
That looks like a useful proposal.
Ok, here is my patch for it.
I tried to test it properly, but please test it as well.
Here is what it looks like:
>>> Schema(os.path.isfile, error='File "{value}" does not exist').validate('/blah')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "schema.py", line 178, in validate
data)
schema.SchemaError: File "/blah" does not exist
Or from what I put in the readme:
>>> Schema(Use(int, error='Invalid year: {value}')).validate('XVII')
Traceback (most recent call last):
...
SchemaError: Invalid year: XVII
More complex example:
>>> try:
... Schema({1: Or(int, float, error='B> {value}')}, 'A> {value}').validate({1:'a'})
... except Exception as e:
... print 'value:', e.value
... print 'message:', e.message.encode('string-escape')
...
value: {1: 'a'}
message: A> {1: \'a\'}\nB> a
>>>
Tell me what you think.
edit: maybe better comment on the PR: #17
I think this is useful, but I'm not 100% about the {value} syntax. Let me think if there is any better alternative.
I'd like that feature - it doesn't seem to be working, is it ?
Hi @c-holtermann, it is not working at the moment, but I've submitted a pull request here. Feel free to test it and share your thoughts there!