Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Allow dot traversing for validators #85

Closed
rochacbruno opened this issue Sep 21, 2018 · 3 comments
Closed

RFC: Allow dot traversing for validators #85

rochacbruno opened this issue Sep 21, 2018 · 3 comments

Comments

@rochacbruno
Copy link
Owner

@rochacbruno rochacbruno commented Sep 21, 2018

Depends on #84

Traversal of nested dictionaries should also be possible in validators.

having settings.yaml

DEFAULT:
    REDIS:
        HOST: localhost
        PORT: 1234

It is currently possible to write validator to ensure:

REDIS key is set and it is a dict and it has the keys HOST and PORT being HOST a str and PORT as int

from dynaconf import settings, Validator

settings.validators.register(
    Validator(
        'REDIS', 
        must_exist=True, 
        is_type_of=dict.
        condition=lambda v: (
            'HOST' in v and 'PORT' in v
        ) and (
            isinstance(v.HOST, int) and isinstance(v.PORT, int)
        )
    ),
)

RFC: Allow the above validation to be written using dot notation

settings.validators.register(
    Validator('REDIS',  must_exist=True,  is_type_of=dict),
    Validator('REDIS.HOST', must_exist=True, is_type_of=str),
    Validator('REDIS.PORT', must_exist=True, is_type_of=int),
)

Also the above validation can be translated to toml validator file as in https://dynaconf.readthedocs.io/en/latest/guides/validation.html#cli-and-dynaconf-validators-toml

@rochacbruno rochacbruno changed the title Allow dot traversing for validators RFC: Allow dot traversing for validators Sep 21, 2018
@rochacbruno rochacbruno added the RFC label Sep 21, 2018
@suvratjain1995
Copy link

@suvratjain1995 suvratjain1995 commented Oct 1, 2018

@rochacbruno I would like to work on this enhancement.

@rochacbruno
Copy link
Owner Author

@rochacbruno rochacbruno commented Oct 1, 2018

@suvratjain1995 cool! this issue depends on the implementation of #84

jperras added a commit to jperras/dynaconf that referenced this issue Oct 22, 2018
One is now able to write:

```python
settings.validators.register(
    Validator('REDIS',  must_exist=True,  is_type_of=dict),
    Validator('REDIS.HOST', must_exist=True, is_type_of=str),
    Validator('REDIS.PORT', must_exist=True, is_type_of=int),
)
```

Which will validate the dotted attributes as nested structures. For
example, in yaml:

```yaml
DEFAULT:
    REDIS:
        HOST: localhost
        PORT: 1234
```

This necessitated a slight but non-negligible change in the
implementation of `Settings.exists()`, which previously did a shallow
check of loaded data. It has now been updated to perform a
`Settings.get()` of the key in question, and compares that to a newly
defined sentinel value to ensure `None` values do not cause a false
negative result.

New tests and assertions have been added to cover the new functionality.
Docs have been updated to show an example of the nested validator name
definition in action.

Closes rochacbruno#85.
jperras added a commit to jperras/dynaconf that referenced this issue Oct 22, 2018
One is now able to write:

```python
settings.validators.register(
    Validator('REDIS',  must_exist=True,  is_type_of=dict),
    Validator('REDIS.HOST', must_exist=True, is_type_of=str),
    Validator('REDIS.PORT', must_exist=True, is_type_of=int),
)
```

Which will validate the dotted attributes as nested structures. For
example, in yaml:

```yaml
DEFAULT:
    REDIS:
        HOST: localhost
        PORT: 1234
```

This necessitated a slight but non-negligible change in the
implementation of `Settings.exists()`, which previously did a shallow
check of loaded data. It has now been updated to perform a
`Settings.get()` of the key in question, and compares that to a newly
defined sentinel value to ensure `None` values do not cause a false
negative result.

New tests and assertions have been added to cover the new functionality.
Docs have been updated to show an example of the nested validator name
definition in action.

Closes rochacbruno#85.
@jperras
Copy link
Contributor

@jperras jperras commented Oct 22, 2018

Sorry for the issue noise – I didn't realize that my forced push rebasing onto my own fork feature branch would cause multiple activity references here. I'll avoid linking the issue no. in commit messages from now on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.