python-dependency-injector icon indicating copy to clipboard operation
python-dependency-injector copied to clipboard

Add Validation feature for loading configuration

Open suspectinside opened this issue 3 years ago • 3 comments

lets say we has some simple config file:

search:
  host: 127.0.0.1
  port: '1234'
  token: '[some token for search backend]'
  report_to: [email protected],
  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: ConfigSearchModel

and 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!

suspectinside avatar Mar 16 '22 13:03 suspectinside

Hi @suspectinside ,

Thanks a lot for the feedback, that totally makes sense. I'll work on it.

rmk135 avatar Mar 25 '22 21:03 rmk135

This would be a great feature!

rowan-maclachlan avatar Apr 19 '22 22:04 rowan-maclachlan

If Pydantic is used to validate, it would be great to keep the config as a BaseSetting object so you can utilize autocomplete on the config when its injected instead of a dictionary.

MattConflitti avatar Jul 28 '22 04:07 MattConflitti