-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Basic set of setting validators.""" | ||
from django.core.exceptions import ValidationError | ||
|
||
|
||
class TypeValidator(object): | ||
"""Validator which checks type of the value.""" | ||
|
||
message = 'Value %(value)s is not of type %(type)s.' | ||
|
||
def __init__(self, value_type, message=None): | ||
self.value_type = value_type | ||
if message: | ||
self.message = message | ||
|
||
def __call__(self, value): | ||
if not isinstance(value, self.value_type): | ||
params = {'value': value, 'type': self.value_type.__name__} | ||
raise ValidationError(self.message, params=params) | ||
|
||
|
||
class ValuesTypeValidator(object): | ||
"""Validator which checks types of iterable values.""" | ||
|
||
message = 'Element %(value)s is not of type %(type)s.' | ||
|
||
def __init__(self, value_type, message=None): | ||
self.value_type = value_type | ||
if message: | ||
self.message = message | ||
|
||
def __call__(self, value): | ||
for element in value: | ||
if not isinstance(element, self.value_type): | ||
params = {'value': element, 'type': self.value_type.__name__} | ||
raise ValidationError(self.message, params=params) | ||
|
||
|
||
class DictValuesTypeValidator(object): | ||
"""Validator which checks types of dict values.""" | ||
|
||
message = "Item %(key)s's value %(value)s is not of type %(type)s." | ||
|
||
def __init__(self, value_type, message=None): | ||
self.value_type = value_type | ||
if message: | ||
self.message = message | ||
|
||
def __call__(self, value): | ||
for key, element in value.items(): | ||
if not isinstance(element, self.value_type): | ||
params = {'key': key, 'value': element, 'type': self.value_type.__name__} | ||
raise ValidationError(self.message, params=params) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Test settings validators.""" | ||
from django.core.exceptions import ValidationError | ||
from django.test import SimpleTestCase | ||
|
||
from appsettings import ( | ||
DictValuesTypeValidator, TypeValidator, ValuesTypeValidator) | ||
|
||
|
||
class TypeValidatorTestCase(SimpleTestCase): | ||
"""Test TypeValidator.""" | ||
|
||
def test_valid(self): | ||
TypeValidator(int)(42) | ||
TypeValidator(float)(4.5) | ||
TypeValidator(list)([]) | ||
|
||
def test_invalid(self): | ||
with self.assertRaisesMessage(ValidationError, "Value None is not of type int."): | ||
TypeValidator(int)(None) | ||
|
||
|
||
class ValuesTypeValidatorTestCase(SimpleTestCase): | ||
"""Test ValuesTypeValidator.""" | ||
|
||
def test_valid(self): | ||
ValuesTypeValidator(int)([42, 14, 1676]) | ||
ValuesTypeValidator(int)({42, 14, 1676}) | ||
ValuesTypeValidator(int)((42, 14, 1676)) | ||
|
||
def test_invalid(self): | ||
with self.assertRaisesMessage(ValidationError, "Element None is not of type int."): | ||
ValuesTypeValidator(int)([42, None, 1676]) | ||
|
||
|
||
class DictValuesTypeValidatorTestCase(SimpleTestCase): | ||
"""Test DictValuesTypeValidator.""" | ||
|
||
def test_valid(self): | ||
DictValuesTypeValidator(int)({'a': 42, 'b': 1676}) | ||
|
||
def test_invalid(self): | ||
with self.assertRaisesMessage(ValidationError, "Item b's value None is not of type int."): | ||
DictValuesTypeValidator(int)({'a': 42, 'b': None}) |