Skip to main content
Version: 1.0

Static schema with many configs

ย Example

We have seen that if the name of the config file matches the name of a configs stored in the ConfigStore it will be used to validate the config file automatically. This is useful if there is a one-to-one mapping between the Structured Configs and the YAML files. Such convenient mapping does not exist when we have many config files and just one schema.

If the config has a static structure, You can define it using Structured Configs. Any config merged into this config structure will be validated against the schema you define.

@dataclassclass DBConfig:    driver: str = MISSING    host: str = MISSING    user: str = MISSING    password: str = MISSING
@dataclassclass Config:    db: DBConfig = MISSING
cs = ConfigStore.instance()"config", node=Config)
@hydra.main(config_path="conf",             config_name="config")def my_app(cfg: Config) -> None:    print(OmegaConf.to_yaml(cfg))
if __name__ == "__main__":    my_app()

Config directory
โ”œโ”€โ”€ config.yamlโ””โ”€โ”€ db    โ”œโ”€โ”€ prod.yaml    โ”œโ”€โ”€ qa.yaml    โ””โ”€โ”€ staging.yaml
defaults:  - db: staging
# @package _group_driver: mysqlhost: mysql001.staginguser: rootpassword: root

In the above example, the 3 yaml files has the structure compatible with the Config dataclass. You can have as many such configs as you want.

$ python db=proddb:  driver: mysql  host:  user: root  password: '1234'