Skip to content

Commit 8cffee7

Browse files
author
Sylvain MARIE
committed
Fixed issue: custom help messages in ValidationError using several variables were not rendering to string correctly and instead were displaying Error while formatting the help message. Fixes #58
1 parent de9bf23 commit 8cffee7

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

valid8/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ def get_help_msg(self):
223223
# first format if needed
224224
try:
225225
is_context_a_copy = False
226-
variables = re.findall("{\\S+}", help_msg)
226+
# note: use question mark so as to be non-greedy
227+
variables = re.findall("{\\S+?}", help_msg)
227228
for var_name_ in set(variables):
228229
# extract the variable name
229230
var_name_ = var_name_[1:-1]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
3+
from valid8 import ValidationError, validate
4+
5+
6+
def test_err_msg_formatting():
7+
"""Test for https://github.com/smarie/python-valid8/issues/58"""
8+
9+
class CustomError(ValidationError):
10+
help_msg = "{a}='{b}': hello"
11+
12+
with pytest.raises(CustomError) as err:
13+
validate('dummy', False, equals=True, error_type=CustomError, a=0, b=1)
14+
15+
assert str(err.value) == "0='1': hello. Error validating [dummy=False]. " \
16+
"NotEqual: x == True does not hold for x=False. " \
17+
"Wrong value: False."

0 commit comments

Comments
 (0)