trafaret icon indicating copy to clipboard operation
trafaret copied to clipboard

Way to redefine error messages

Open lud4ik opened this issue 7 years ago • 6 comments

I have a scheme of t.Dict with several fields. I want when required field is absent to replace default error string. What is the best way to do it? without kostyls...

lud4ik avatar Feb 10 '19 21:02 lud4ik

U can redefine base clases.

class MyBool(t.Bool):
    def check_value(self, value):
        if not isinstance(value, bool):
            self._failure("My custom error", value=value)

MyBool().check(True)

or user t.Call

def not_some_validator(value):
    if value == "some":
        return t.DataError('My custom error.')
    return value

t.Call(not_some_validator).check("some")

I'm not sure what is the best way but it's not look like "kostyls"

Arfey avatar Feb 11 '19 17:02 Arfey

I need some time to figure out proper way.

Deepwalker avatar Feb 11 '19 18:02 Deepwalker

а можна ли разширить так чтобы все текста ошибок можна было переопределять? к примеру если t.Int(gt=0) при 0 задать "Please enter non-zero value"

lud4ik avatar Mar 01 '19 09:03 lud4ik

можно, на выходных постараюсь сделать

Deepwalker avatar Mar 01 '19 15:03 Deepwalker

Привет, это еще актуально? Тоже интересует ..

hzlmn avatar Dec 11 '19 17:12 hzlmn

In version2 DataError has code field and to_struct method that returns code as well as message. It can be used with any localisation library. I think this should solve localisation problem.

Deepwalker avatar Dec 12 '19 13:12 Deepwalker