Deepwalker / trafaret

Ultimate transformation library that supports validation, contexts and aiohttp.
http://trafaret.readthedocs.org/en/latest/
BSD 2-Clause "Simplified" License
177 stars 31 forks source link

Way to redefine error messages #48

Open lud4ik opened 5 years ago

lud4ik commented 5 years ago

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...

Arfey commented 5 years ago

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"

Deepwalker commented 5 years ago

I need some time to figure out proper way.

lud4ik commented 5 years ago

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

Deepwalker commented 5 years ago

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

hzlmn commented 4 years ago

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

Deepwalker commented 4 years ago

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.