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

Schema based validation part 1 #677

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft

Schema based validation part 1 #677

wants to merge 11 commits into from

Conversation

rochacbruno
Copy link
Member

@rochacbruno rochacbruno commented Oct 12, 2021

MIssing:

  • docs
  • functional examples
  • runtime type check
  • experiment with using the schema as inversed object
from dataclasses import dataclass
from typing import List

from dynaconf import Dynaconf
from dynaconf import ExtraFields
from dynaconf import Field
from dynaconf import Schema
from dynaconf import Validator

"""
settings.json
{
    "random_thing": True,
    "ignored_from_json": True,
}
"""

email_expr = "(this.name + '@' + this.company + env.TLD) | lower"


@dataclass
class MySchema(Schema):
    city: str
    schema: int
    a_thing: List = Field(default_factory=list)
    random_thing: bool = False  # will come from settings.json
    computed: int = Field(default_factory=lambda: 40 + 2)
    name_upper: str = Field(default_expr="this.name.upper()")
    name_reversed: str = Field(default_expr="this.name | reverse")
    company: str = "Acme"
    email: str = Field(default_expr=email_expr, force_default=True)
    name: str = Field(
        validators=["this.name != 'Bruno' and env.IS_FOO == 'this_is_foo'"]
    )
    age: int = Field(18, validators=[Validator(gt=1, lt=16)])
    banana: str = Field(validators=[lambda this: this.city == "Rio"])

    class Config:
        extra_fields_policy = ExtraFields.ignore

settings = Dynaconf(
    settings_file="settings.json",
    envvar_prefix="DYNACONF_",
    environments=False,
    load_dotenv=False,
    name="Bruno2",
    city="Rio",
    banana=1,
    age=15,
    schema=1,
    abcd=10,  # this will be ignored, because it's not in the schema
    dynaconf_schema=MySchema,
    random_thing=False,  # Json wins
)

assert settings.name == "Bruno2"
assert "NAME" in settings
assert "name" in settings
assert settings.name_upper == "BRUNO2"
assert settings.name_reversed == "2onurB"
assert settings.company == "Acme"
assert settings.email == "[email protected]"
assert settings.age == 15
assert settings.computed == 42
assert settings.random_thing is True

assert settings.load_dotenv_for_dynaconf is False

assert "ABCD" not in settings, settings.ABCD
assert "IGNORED" not in settings, settings.IGNORED
assert "IGNORED_FROM_JSON" not in settings, settings.IGNORED_FROM_JSON

@pep8speaks
Copy link

pep8speaks commented Oct 12, 2021

Hello @rochacbruno! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 74:19: E701 multiple statements on one line (colon)
Line 74:19: E231 missing whitespace after ':'
Line 74:20: E225 missing whitespace around operator
Line 74:20: E999 SyntaxError: invalid syntax

Comment last updated at 2021-10-18 20:38:01 UTC

@github-actions
Copy link
Contributor

github-actions bot commented Oct 13, 2021

Test Results (Python 3.8)

    1 files  ±  0      1 suites  ±0   36s ⏱️ -3s
373 tests +29  373 ✔️ +29  0 💤 ±0  0 ±0 

Results for commit 9bf7b77. ± Comparison against base commit 4a6705e.

♻️ This comment has been updated with latest results.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 13, 2021

Test Results (Python 3.10)

    1 files  ±  0      1 suites  ±0   41s ⏱️ -1s
373 tests +29  373 ✔️ +29  0 💤 ±0  0 ±0 

Results for commit 9bf7b77. ± Comparison against base commit 4a6705e.

♻️ This comment has been updated with latest results.

@codecov
Copy link

codecov bot commented Oct 13, 2021

Codecov Report

Merging #677 (9bf7b77) into master (f3da26c) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##            master      #677    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           21        22     +1     
  Lines         1667      1928   +261     
==========================================
+ Hits          1667      1928   +261     
Impacted Files Coverage Δ
dynaconf/loaders/env_loader.py 100.00% <ø> (ø)
dynaconf/__init__.py 100.00% <100.00%> (ø)
dynaconf/base.py 100.00% <100.00%> (ø)
dynaconf/schema.py 100.00% <100.00%> (ø)
dynaconf/utils/parse_conf.py 100.00% <100.00%> (ø)
dynaconf/validator.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f3da26c...9bf7b77. Read the comment docs.

@stale
Copy link

stale bot commented Jul 14, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

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

Successfully merging this pull request may close these issues.

None yet

2 participants