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()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.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
You can have as many such configs as you want.
$ python my_app.py db=proddb: driver: mysql host: mysql001.prod user: root password: '1234'