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] Pydantic Schema Validation #1045

Open
rochacbruno opened this issue Jan 26, 2024 · 3 comments
Open

[RFC] Pydantic Schema Validation #1045

rochacbruno opened this issue Jan 26, 2024 · 3 comments
Labels
Not a Bug Not a Problem, expected behavior RFC

Comments

@rochacbruno
Copy link
Member

rochacbruno commented Jan 26, 2024

This RFE has potential to replace the proposal I made on #683

Which is the support for the following snippet

from pydantic import BaseModel, ConfigDict
from dynaconf import Dynaconf, DynaBox


class MySchema(BaseModel):
    model_config = ConfigDict(extra='allow')
	dynaconf_config = DynaBox(lazy_validation=True, settings_files=["settings.yaml"])

    server: str
    port: int


settings = Dynaconf(
    schema=MySchema,  # replaces `validators` and is mutually exclusive
)

settings.set("server", 1234)  
# don't raise because lazy_validation=True
# if lazy_validation was false then .set would assume the need for validation

print(settings.server)  
# raise validation error because settings is evaluated on the first attr access

Details:

  • Must keep the current validators working with no regression, the only caveat is that the user must choose only one validation strategy to use to avoid confusions
  • On Django the schema must be defined(or imported) inside the app/settings.py and passed to DjangoDynaconf, the same for FlaskDynaconf.
  • If validation is not lazy, then it will be called for every .set or .update, we have to ensure that inside loaders we can load multiple keys in bulk and validate once
  • Pydantic is not added as a hard dependency, can be added on the extra section, but on the docs it must be described to install pydantic along with Dynaconf
  • Support for Pydantic v2 only
  • Before invoking the validation, the keys on settings.as_dict() must be transformed to the proper case, the way it is defined on the schema (not sure if pydantic allows case insensitivity)
@rochacbruno rochacbruno added Not a Bug Not a Problem, expected behavior RFC labels Jan 26, 2024
@rochacbruno
Copy link
Member Author

Some of this has been already discussed on #608

@pedro-psb
Copy link
Member

There is a downside I overlooked about using composition over inheritance that I've overlooked in our last discussion: we don't get auto-complete out of the box (with composition), which is a very requested feature related to schema-based config.

@rochacbruno
Copy link
Member Author

@pedro-psb we can fix that with composition, python is very permissive.

2024-01-30_16-28_1
2024-01-30_16-28
2024-01-30_16-26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Bug Not a Problem, expected behavior RFC
Projects
None yet
Development

No branches or pull requests

2 participants