diff --git a/tests/test_validation.py b/tests/test_validation.py index a79d61b1..99b9eb56 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -18,7 +18,7 @@ make_app, setup_session_dir, teardown_session_dir) from tg._compat import PY3, unicode_text, u_, default_im_func -from tg.validation import TGValidationError, _ValidationStatus, Convert +from tg.validation import TGValidationError, _ValidationStatus, Convert, RequireValue from tg.i18n import lazy_ugettext as l_ @@ -318,6 +318,13 @@ def chain_validation_2(self, val): def chain_validation_begin(self, val): return '>3' + @expose(content_type='text/plain') + @validate({ + 'val': RequireValue(msg=l_("Value is required")) + }, error_handler=validation_errors_response) + def require_value(self, val=None): + return val + class TestTGController(TestWSGIController): def setup_method(self): @@ -505,6 +512,13 @@ def test_validation_errors_lazy_unicode(self): resp = self.app.post('/lazy_unicode_error_pow', {'num': 'NOT_A_NUMBER'}, status=412) assert resp.json['errors']['num'] == u_('àèìòù'), resp.json + def test_requirevalue_validation(self): + resp = self.app.post('/require_value', {"val": "hello"}) + assert resp.text == 'hello', resp + + resp = self.app.post('/require_value', {}, status=412) + assert resp.json["errors"]["val"] == 'Value is required', resp + class TestChainValidation(TestWSGIController): def setup_method(self): diff --git a/tg/validation.py b/tg/validation.py index 169da4aa..d22eafd6 100644 --- a/tg/validation.py +++ b/tg/validation.py @@ -1,5 +1,6 @@ import warnings +from tg.configuration.utils import TGConfigError from tg._compat import unicode_text from .i18n import lazy_ugettext @@ -89,7 +90,7 @@ def check(self, config, method, params): validation_function = validation_validators[supported_class] if validation_function is None: - raise TGConfigError(f"No validation validator function found for: {exception_class}") + raise TGConfigError(f"No validation validator function found for: {schema_class}") validated_params = validation_function(validators, params)