Skip to content

Commit

Permalink
Add new validation to fix issue #585 (#667)
Browse files Browse the repository at this point in the history
Co-authored-by: andressa.cabistani <andressa.cabistani@thoughtworks.com>
  • Loading branch information
andressadotpy and acabistani committed Oct 8, 2021
1 parent 7cab97c commit 79f5088
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
17 changes: 17 additions & 0 deletions docs/validation.md
Expand Up @@ -167,6 +167,23 @@ $ dynaconf validate
This returns code 0 (success) if validation is ok.


!!! info
All values in dynaconf are parsed using toml format, TOML tries to be smart
and infer the type of the settings variables, some variables will be automatically
converted to integer:

FOO = "0x..." # hexadecimal
FOO = "0o..." # Octal
FOO = "0b..." # Binary

All cases are on toml specs https://github.com/toml-lang/toml/blob/master/toml.abnf

If you need to force a specific type casting there are 2 options.

1. Use double quoted for strings ex: `FOO = "'0x...'" will be string.
2. Specify the type using `@` ex: FOO = "@str 0x..."
(available converters are `@int, @float, @bool, @json`)

## Selective Validation

> **New in 3.1.6**
Expand Down
7 changes: 7 additions & 0 deletions dynaconf/utils/parse_conf.py
Expand Up @@ -316,6 +316,13 @@ def parse_conf_data(data, tomlfy=False, box_settings=None):
)
return _parsed

if (
isinstance(data, str)
and data.startswith(("+", "-"))
and data[1:].isdigit()
):
return data

# return parsed string value
return _parse_conf_data(data, tomlfy=tomlfy, box_settings=box_settings)

Expand Down
17 changes: 17 additions & 0 deletions tests/test_validators.py
Expand Up @@ -2,6 +2,7 @@

import pytest

from dynaconf import Dynaconf
from dynaconf import LazySettings
from dynaconf import ValidationError
from dynaconf import Validator
Expand Down Expand Up @@ -659,3 +660,19 @@ def test_validator_descriptions_flat(tmpdir):
"a": "a",
"b": "a",
}


def test_toml_should_not_change_validator_type_with_is_type_set():
settings = Dynaconf(
validators=[Validator("TEST", is_type_of=str, default="+172800")]
)

assert settings.test == "+172800"


def test_toml_should_not_change_validator_type_using_at_sign():
settings = Dynaconf(
validators=[Validator("TEST", is_type_of=str, default="@str +172800")]
)

assert settings.test == "+172800"

0 comments on commit 79f5088

Please sign in to comment.