Skip to content

Add Validation feature for loading configuration #568

Open
@suspectinside

Description

@suspectinside

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions