Fix config validation for String types.

Fixes #1231.
This commit is contained in:
Florian Bruhin 2016-01-18 07:29:15 +01:00
parent f7784e641e
commit c8538bdbb8
2 changed files with 11 additions and 0 deletions

View File

@ -258,6 +258,13 @@ class String(BaseType):
self._basic_validation(value)
if not value:
return
if self.valid_values is not None:
if value not in self.valid_values:
raise configexc.ValidationError(
value, "valid values: {}".format(', '.join(
self.valid_values)))
if self.forbidden is not None and any(c in value
for c in self.forbidden):
raise configexc.ValidationError(value, "may not contain the chars "

View File

@ -305,6 +305,8 @@ class TestString:
({'minlen': 2}, 'fo'),
({'minlen': 2, 'maxlen': 3}, 'fo'),
({'minlen': 2, 'maxlen': 3}, 'foo'),
# valid_values
({'valid_values': configtypes.ValidValues('fooo')}, 'fooo'),
])
def test_validate_valid(self, klass, kwargs, val):
klass(**kwargs).validate(val)
@ -319,6 +321,8 @@ class TestString:
({'maxlen': 2}, 'fob'),
({'minlen': 2, 'maxlen': 3}, 'f'),
({'minlen': 2, 'maxlen': 3}, 'fooo'),
# valid_values
({'valid_values': configtypes.ValidValues('blah')}, 'fooo'),
])
def test_validate_invalid(self, klass, kwargs, val):
with pytest.raises(configexc.ValidationError):