custom error messages

3 views
Skip to first unread message

Burak Cankurtaran

unread,
Jan 6, 2011, 9:20:29 AM1/6/11
to ish.io
Hi,

I'm only using validatish and schemaish in my application.
I want to be able to display any validation errors using
custom messages stored in the schemaish structure.
I could change the default messages in validatish but
then there is no schema dependence.

Has anyone extended schemaish to support this?

I think formish supports this but I am not
using formish. I'm a new user so not quite sure.

Thanks
Burak

Daniel Holth

unread,
Jan 6, 2011, 9:41:29 AM1/6/11
to is...@googlegroups.com
I have found I always need to override the default error messages. I simply wrap the validator in a class that provides the message I want:

class Message(object):
    def __init__(self, context, message):
        self.context = context
        self.message = message

    def __call__(self, data):
        try:
            self.context(data)
        except validatish.Invalid:
            raise validatish.Invalid(self.message)

Matt Goodall

unread,
Jan 6, 2011, 10:10:52 AM1/6/11
to is...@googlegroups.com
On 6 January 2011 14:20, Burak Cankurtaran <burak...@gmail.com> wrote:
Hi,

I'm only using validatish and schemaish in my application.
I want to be able to display any validation errors using
custom messages stored in the schemaish structure.
I could change the default messages in validatish but
then there is no schema dependence.

Has anyone extended schemaish to support this?

It's really validatish that handles the error messages.

It's not something I've ever done but you should be able to pass a dict of custom error messages to any validatish function or validator instance. For example:

>>> import validatish
>>> validatish.is_in_range(10, min=18)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/matt/src/jdi/profiled/libs/validatish/validatish/validate.py", line 281, in is_in_range
    raise Invalid(error)
validatish.error.Invalid: must be greater than or equal to 18
>>> validatish.is_in_range(10, min=18, messages={'greater-than': 'You must be %(min)s or older'})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/matt/src/jdi/profiled/libs/validatish/validatish/validate.py", line 281, in is_in_range
    raise Invalid(error)
validatish.error.Invalid: You must be 18 or older
>>> 

- Matt

 

I think formish supports this but I am not
using formish. I'm a new user so not quite sure.

Thanks
Burak

--
You received this message because you are subscribed to the Google Groups "ish.io" group.
To post to this group, send an email to is...@googlegroups.com.
To unsubscribe from this group, send email to ishio+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ishio?hl=en-GB.


Burak Cankurtaran

unread,
Jan 6, 2011, 10:40:44 AM1/6/11
to is...@googlegroups.com
Thanks Daniel and Matt.

Just quickly, I just unsuccessfully tried Matt's suggestion;

>>> validatish.is_in_range(10, min=18, messages={'greater-than': 'You must be %(min)s or older'})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>

File "build/bdist.linux-x86_64/egg/validatish/validate.py", line
286, in is_in_range


validatish.error.Invalid: must be greater than or equal to 18

I think there might be a difference in the library versions. Matt the
exception is raised
on line 281 for your version and line 286 for my version.

I used easy_install to install the library - validatish-0.6.4-py2.7.egg

After I get some sleep I'll look at both solutions properly.

Thanks
Burak

Matt Goodall

unread,
Jan 6, 2011, 11:21:15 AM1/6/11
to is...@googlegroups.com
On 6 January 2011 15:40, Burak Cankurtaran <burak...@gmail.com> wrote:
Thanks Daniel and Matt.

Just quickly, I just unsuccessfully tried Matt's suggestion;

>>> validatish.is_in_range(10, min=18, messages={'greater-than': 'You must be %(min)s or older'})
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "build/bdist.linux-x86_64/egg/validatish/validate.py", line
286, in is_in_range
validatish.error.Invalid: must be greater than or equal to 18

I think there might be a difference in the library versions. Matt the
exception is raised
on line 281 for your version and line 286 for my version.

Sorry, that's my fault. I fixed that particular validation function in the git repository but there's been no release since. It's just typical that I chose the only validation function that was broken for the example :/.

I'll try to get a new release out, but you can get the latest source from https://github.com/ish/validatish/tree/ until then.

- Matt
Reply all
Reply to author
Forward
0 new messages