16.01.18 11:38, smarie пише:
You can find my attempt to do that in the valid8 project https://smarie.github.io/python-valid8 https://smarie.github.io/python-valid8, with the 'assert_valid(...)' function. With the current language limitations I could not define something as simple as 'validate <expression> <exception, exception_type or exception_message>', but I created a mini lambda library to at least keep some level of simplicity for the <expression>. The result is for example :
class InvalidSurface(ValidationError): help_msg = 'Surface should be a positive number'
assert_valid('surface', surf, x > 0, error_type=InvalidSurface)
What is the advantage over the simple
if not x > 0: raise InvalidSurface('surface')