-
- Notifications
You must be signed in to change notification settings - Fork 339
Description
lets say we has some simple config file:
search: host: 127.0.0.1 port: '1234' token: '[some token for search backend]' report_to: main@domain.com, some_repo: https://backend.com,and we want that it would already be validated and casted automatically, not just loaded.
PDI uses Pydantic already, so i'd like to suggest to add validation parameter with Pydantic Model which would be used for validation and before internal Configuration structures will be populated.
for the above yaml config we have Pydantic model like this:
class ConfigSearchModel(BaseModel): host: str port: int token: str report_to: EmailStr, some_repo: AnyHttpUrl, mq_dsn: RabbitMqDsn = Field(None) class ConfigModel(BaseModel): search: ConfigSearchModeland PDI part:
class Container(containers.DeclarativeContainer): config = providers.Configuration() # ... some other providers if __name__ == "__main__": container = Container() container.config.from_yaml("./config.yml", pydantic_validation_model=ConfigModel)the new parameter pydantic_validation_model=ConfigModel shows which Pydantic model (if any) to use for validation and values casting
we don't need .as_int, .as_ etc but we have the full power of Pydantic validation and casting engine out of the box instantly.
we may be sure that 'config.port' will be int, report_to will contain e-mail, some_repo - valid http url etc, and not to spend time for type checking, casting in container or somewhere else.
Thanks!