schema icon indicating copy to clipboard operation
schema copied to clipboard

Reference values in error messages.

Open apalala opened this issue 13 years ago • 5 comments

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')]
                                  ...

apalala avatar Oct 15 '12 15:10 apalala

That looks like a useful proposal.

keleshev avatar Oct 15 '12 15:10 keleshev

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

Rogdham avatar May 13 '13 18:05 Rogdham

I think this is useful, but I'm not 100% about the {value} syntax. Let me think if there is any better alternative.

keleshev avatar May 13 '13 21:05 keleshev

I'd like that feature - it doesn't seem to be working, is it ?

c-holtermann avatar Oct 24 '13 17:10 c-holtermann

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!

Rogdham avatar Oct 24 '13 18:10 Rogdham