Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_dotted_validators(settings):
settings.set(
"PARAMS",
{"PASSWORD": "secret", "SSL": {"CONTEXT": "SECURE", "ENABLED": True}},
)
settings.validators.register(
Validator("PARAMS", must_exist=True, is_type_of=dict),
Validator("PARAMS.PASSWORD", must_exist=True, is_type_of=str),
Validator("PARAMS.SSL", must_exist=True, is_type_of=dict),
Validator("PARAMS.SSL.ENABLED", must_exist=True, is_type_of=bool),
Validator("PARAMS.NONEXISTENT", must_exist=False, is_type_of=str),
)
assert settings.validators.validate() is None
Validator("ZERO", is_type_of=int, eq=0),
Validator("FALSE", is_type_of=bool, eq=False),
)
assert settings.validators.validate() is None
settings.validators.register(
Validator("TESTVALUEZZ", env="development"),
Validator("TESTVALUE", eq="hello_world"),
)
with pytest.raises(ValidationError):
settings.validators.validate()
with pytest.raises(TypeError):
Validator("A", condition=1)
with pytest.raises(TypeError):
Validator("A", when=1)
def test_validators(tmpdir):
tmpfile = tmpdir.join("settings.toml")
tmpfile.write(TOML)
settings = LazySettings(
ENV_FOR_DYNACONF="EXAMPLE",
SETTINGS_FILE_FOR_DYNACONF=str(tmpfile),
silent=True,
)
settings.validators.register(
Validator("VERSION", "AGE", "NAME", must_exist=True),
Validator("AGE", lte=30, gte=10),
Validator("PROJECT", eq="hello_world"),
Validator("PRESIDENT", env="DEVELOPMENT", ne="Trump"),
Validator("SALARY", lt=1000000, gt=1000),
Validator("DEV_SERVERS", must_exist=True, is_type_of=(list, tuple)),
Validator("MYSQL_HOST", env="DEVELOPMENT", is_in=settings.DEV_SERVERS),
Validator(
"MYSQL_HOST", env="PRODUCTION", is_not_in=settings.DEV_SERVERS
),
Validator("NAME", condition=lambda x: x in ("BRUNO", "MIKE")),
Validator(
"IMAGE_1",
"IMAGE_2",
env="development",
must_exist=True,
when=Validator(
"BASE_IMAGE", must_exist=True, env=settings.ENV_FOR_DYNACONF
def test_equality():
validator1 = Validator("VERSION", "AGE", "NAME", must_exist=True)
validator2 = Validator("VERSION", "AGE", "NAME", must_exist=True)
assert validator1 == validator2
assert validator1 is not validator2
validator3 = (
Validator("IMAGE_1", when=Validator("BASE_IMAGE", must_exist=True)),
)
validator4 = (
Validator("IMAGE_1", when=Validator("MYSQL_HOST", must_exist=True)),
)
assert validator3 != validator4
env="development",
must_exist=True,
when=Validator(
"BASE_IMAGE", must_exist=True, env=settings.ENV_FOR_DYNACONF
),
),
Validator(
"IMAGE_4",
"IMAGE_5",
env=("development", "production"),
must_exist=True,
when=Validator(
"BASE_IMAGE", must_exist=False, env=settings.ENV_FOR_DYNACONF
),
),
Validator(
"PORT",
must_exist=True,
ne=8000,
when=Validator("MYSQL_HOST", eq="localhost"),
),
#
Validator("ZERO", is_type_of=int, eq=0),
Validator("FALSE", is_type_of=bool, eq=False),
)
assert settings.validators.validate() is None
settings.validators.register(
Validator("TESTVALUEZZ", env="development"),
Validator("TESTVALUE", eq="hello_world"),
)
SETTINGS_FILE_FOR_DYNACONF=str(tmpfile),
silent=True,
)
settings.validators.register(
Validator("VERSION", "AGE", "NAME", must_exist=True),
Validator("AGE", lte=30, gte=10),
Validator("PROJECT", eq="hello_world"),
Validator("PRESIDENT", env="DEVELOPMENT", ne="Trump"),
Validator("SALARY", lt=1000000, gt=1000),
Validator("DEV_SERVERS", must_exist=True, is_type_of=(list, tuple)),
Validator("MYSQL_HOST", env="DEVELOPMENT", is_in=settings.DEV_SERVERS),
Validator(
"MYSQL_HOST", env="PRODUCTION", is_not_in=settings.DEV_SERVERS
),
Validator("NAME", condition=lambda x: x in ("BRUNO", "MIKE")),
Validator(
"IMAGE_1",
"IMAGE_2",
env="development",
must_exist=True,
when=Validator(
"BASE_IMAGE", must_exist=True, env=settings.ENV_FOR_DYNACONF
),
),
Validator(
"IMAGE_4",
"IMAGE_5",
env=("development", "production"),
must_exist=True,
when=Validator(
"BASE_IMAGE", must_exist=False, env=settings.ENV_FOR_DYNACONF
Validator("TESTVALUEZZ", must_exist=True),
],
)
def test_validation_error(validator_instance, tmpdir):
tmpfile = tmpdir.join("settings.toml")
tmpfile.write(TOML)
settings = LazySettings(
ENV_FOR_DYNACONF="EXAMPLE",
SETTINGS_FILE_FOR_DYNACONF=str(tmpfile),
silent=True,
)
settings.validators.register(validator_instance)
with pytest.raises(ValidationError):
settings.validators.validate()
"IMAGE_4",
"IMAGE_5",
env=("development", "production"),
must_exist=True,
when=Validator(
"BASE_IMAGE", must_exist=False, env=settings.ENV_FOR_DYNACONF
),
),
Validator(
"PORT",
must_exist=True,
ne=8000,
when=Validator("MYSQL_HOST", eq="localhost"),
),
#
Validator("ZERO", is_type_of=int, eq=0),
Validator("FALSE", is_type_of=bool, eq=False),
)
assert settings.validators.validate() is None
settings.validators.register(
Validator("TESTVALUEZZ", env="development"),
Validator("TESTVALUE", eq="hello_world"),
)
with pytest.raises(ValidationError):
settings.validators.validate()
with pytest.raises(TypeError):
Validator("A", condition=1)
from dynaconf import LazySettings
from dynaconf import Validator
settings = LazySettings(ENV_FOR_DYNACONF="EXAMPLE")
settings.validators.register(
Validator("VERSION", "AGE", "NAME", must_exist=True),
Validator("AGE", lte=30, gte=10),
Validator("PROJECT", eq="hello_world"),
Validator("PRESIDENT", env="DEVELOPMENT", ne="Trump"),
Validator("SALARY", lt=1000000, gt=1000),
Validator("DEV_SERVERS", must_exist=True, is_type_of=(list, tuple)),
Validator("MYSQL_HOST", env="DEVELOPMENT", is_in=settings.DEV_SERVERS),
Validator("MYSQL_HOST", env="PRODUCTION", is_not_in=settings.DEV_SERVERS),
Validator("NAME", condition=lambda x: x in ("BRUNO", "MIKE")),
Validator(
"IMAGE_1",
"IMAGE_2",
env="development",
must_exist=True,
when=Validator(
"BASE_IMAGE", must_exist=True, env=settings.ENV_FOR_DYNACONF
),
),
Validator(
from dynaconf import LazySettings
from dynaconf import Validator
settings = LazySettings(ENV_FOR_DYNACONF="EXAMPLE")
settings.validators.register(
Validator("VERSION", "AGE", "NAME", must_exist=True),
Validator("AGE", lte=30, gte=10),
Validator("PROJECT", eq="hello_world"),
Validator("PRESIDENT", env="DEVELOPMENT", ne="Trump"),
Validator("SALARY", lt=1000000, gt=1000),
Validator("DEV_SERVERS", must_exist=True, is_type_of=(list, tuple)),
Validator("MYSQL_HOST", env="DEVELOPMENT", is_in=settings.DEV_SERVERS),
Validator("MYSQL_HOST", env="PRODUCTION", is_not_in=settings.DEV_SERVERS),
Validator("NAME", condition=lambda x: x in ("BRUNO", "MIKE")),
Validator(
"IMAGE_1",
"IMAGE_2",
env="development",
must_exist=True,
when=Validator(
"BASE_IMAGE", must_exist=True, env=settings.ENV_FOR_DYNACONF
),
),
Validator(
"IMAGE_4",
"IMAGE_5",
env=("development", "production"),