Version: Next

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.

@dataclass
class DBConfig:
driver: str = MISSING
host: str = MISSING
user: str = MISSING
password: str = MISSING
@dataclass
class Config:
db: DBConfig = MISSING
cs = ConfigStore.instance()
cs.store(name="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
config.yaml
defaults:
- db: staging
db/staging.yaml
# @package _group_
driver: mysql
host: mysql001.staging
user: root
password: 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.

Output
$ python my_app.py db=prod
db:
driver: mysql
host: mysql001.prod
user: root
password: '1234'
Last updated on by Omry Yadan