Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ An example of one might be returning a datastructure expected by a framework:
'LOCATION': '127.0.0.1:6379:0', 'OPTIONS': {'PASSWORD': 'redispass'}}


Value Validators
~~~~~~~~~~~~~~~~
Value validators are callables that are run on the environment variable after a
preprocessor, type casting and postprocessor. If the return value of the validator
is falsy, error will be raised.

.. code-block:: python

import logging

to_upper = lambda v: v.upper()

env('LOG_LEVEL', preprocessor=to_upper, validator=lambda v: hasattr(logging, v))
env.int('PORT', validator=lambda v: v > 0 and v < 65535)


Environment File
~~~~~~~~~~~~~~~~
Read from a .env file (line delimited KEY=VALUE):
Expand All @@ -191,6 +207,7 @@ To run the tests install tox::

pip install tox

Then run them with::
Then run them with:

make test

10 changes: 8 additions & 2 deletions envparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import urlparse


__version__ = '0.2.0'
__version__ = '0.3.0'


logger = logging.getLogger(__file__)
Expand Down Expand Up @@ -57,7 +57,8 @@ def __init__(self, **schema):
self.schema = schema

def __call__(self, var, default=NOTSET, cast=None, subcast=None,
force=False, preprocessor=None, postprocessor=None):
force=False, preprocessor=None, postprocessor=None,
validator=None):
"""
Return value for given environment variable.

Expand All @@ -69,6 +70,8 @@ def __call__(self, var, default=NOTSET, cast=None, subcast=None,
:param force: force to cast to type even if default is set.
:param preprocessor: callable to run on pre-casted value.
:param postprocessor: callable to run on casted value.
:param validator: callable which should return truthy value, otherwise
ConfigurationError will be thrown.

:returns: Value from environment or default (if set).
"""
Expand Down Expand Up @@ -111,6 +114,9 @@ def __call__(self, var, default=NOTSET, cast=None, subcast=None,
value = self.cast(value, cast, subcast)
if postprocessor:
value = postprocessor(value)
if validator and not validator(value):
raise ConfigurationError("Value '{}' is invalid for variable '{}'"
.format(value, var))
return value

@classmethod
Expand Down
9 changes: 9 additions & 0 deletions tests/test_casts.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ def django_redis(url):
postprocessor=django_redis))


def test_validator():
env.int('INT', validator=lambda v: v in [41, 42])
env.json('JSON', validator=lambda v: 'foo' in v)
with pytest.raises(ConfigurationError):
env.int('INT', validator=lambda v: v in [1, 2])
with pytest.raises(ConfigurationError):
env.json('JSON', validator=lambda v: 'bar' in v)


def test_schema():
env = Env(STR=str, STR_DEFAULT=dict(cast=str, default='default'),
INT=int, LIST_STR=list, LIST_INT=dict(cast=list, subcast=int))
Expand Down