Skip to content

Commit

Permalink
Merge pull request #65 from loganasherjones/issue/45
Browse files Browse the repository at this point in the history
Added custom validators. Fixes #43
  • Loading branch information
loganasherjones committed May 22, 2018
2 parents a09525f + 07785f8 commit 8b383b1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
8 changes: 8 additions & 0 deletions tests/items_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,11 @@ def test_from_specification_check_fq_names():
items['foo'].children['bar'].children['baz'].fq_name == 'foo.bar.baz'
)
assert items['foo'].children['bat'].fq_name == 'foo.bat'


def test_invalid_value():
def always_invalid(value):
return False
item = from_specification({'foo': {'validator': always_invalid}})['foo']
with pytest.raises(YapconfValueError):
item.get_config_value([('label', {'foo': 'bar'})])
26 changes: 19 additions & 7 deletions yapconf/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def _generate_item(name, item_dict, env_prefix,
init_args['env_prefix'] = env_prefix
init_args['choices'] = item_dict.get('choices', None)
init_args['alt_env_names'] = item_dict.get('alt_env_names', [])
init_args['validator'] = item_dict.get('validator')

if parent_names:
init_args['prefix'] = separator.join(parent_names)
Expand Down Expand Up @@ -190,7 +191,8 @@ def __init__(
apply_env_prefix=True,
choices=None,
alt_env_names=None,
long_description=None
long_description=None,
validator=None
):

self.name = name
Expand All @@ -213,6 +215,7 @@ def __init__(
self.env_prefix = env_prefix or ''
self.apply_env_prefix = apply_env_prefix
self.choices = choices
self.validator = validator

if self.prefix:
self.fq_name = self.separator.join([self.prefix, self.name])
Expand Down Expand Up @@ -353,6 +356,9 @@ def _validate_value(self, value):
raise YapconfValueError("Invalid value provided (%s) for %s."
"Valid values are %s" %
(value, self.fq_name, self.choices))
if self.validator and not self.validator(value):
raise YapconfValueError('Invalid value provided (%s) for %s.' %
(value, self.fq_name))

def convert_config_value(self, value, label):
try:
Expand Down Expand Up @@ -595,7 +601,8 @@ def __init__(
apply_env_prefix=True,
choices=None,
alt_env_names=None,
long_description=None
long_description=None,
validator=None
):
super(YapconfBoolItem, self).__init__(
name,
Expand All @@ -619,7 +626,8 @@ def __init__(
apply_env_prefix,
choices,
alt_env_names,
long_description
long_description,
validator
)

def add_argument(self, parser, bootstrap=False):
Expand Down Expand Up @@ -748,7 +756,8 @@ def __init__(
apply_env_prefix=True,
choices=None,
alt_env_names=None,
long_description=None
long_description=None,
validator=None
):

super(YapconfListItem, self).__init__(
Expand All @@ -773,7 +782,8 @@ def __init__(
apply_env_prefix,
choices,
alt_env_names,
long_description
long_description,
validator
)

if len(self.children) != 1:
Expand Down Expand Up @@ -916,7 +926,8 @@ def __init__(
apply_env_prefix=True,
choices=None,
alt_env_names=None,
long_description=None
long_description=None,
validator=None
):

super(YapconfDictItem, self).__init__(
Expand All @@ -941,7 +952,8 @@ def __init__(
apply_env_prefix,
choices,
alt_env_names,
long_description
long_description,
validator
)

if self.choices is not None:
Expand Down

0 comments on commit 8b383b1

Please sign in to comment.