Validators#
from pprint import pprint
from dicfg import ConfigReader
Use validators with @validate(validator-name) in config files#
!cat ./configs/config_with_validators.yml
config_reader = ConfigReader(name="myconfig", main_config_path="./configs/config_with_validators.yml")
pprint(config_reader.read(), sort_dicts=False)
default:
config_int@validate(positive-number): 1
config_string: "fire"
config_list: [1, 2, 3]
config_none: None
config_dict:
sub_config: "water"{'default': {'config_int': 1,
'config_string': 'fire',
'config_list': [1, 2, 3],
'config_none': 'None',
'config_dict': {'sub_config': 'water'}}}
user_config = {
"myconfig": {
"default": {
"config_int": -1,
}
}
}
pprint(config_reader.read(user_config=user_config), sort_dicts=False)
---------------------------------------------------------------------------
ValidationErrors Traceback (most recent call last)
Cell In[10], line 8
1 user_config = {
2 "myconfig": {
3 "default": {
(...)
6 }
7 }
----> 8 pprint(config_reader.read(user_config=user_config), sort_dicts=False)
File ~/code/dicfg/dicfg/reader.py:128, in ConfigReader.read(self, user_config, presets)
125 merged_configs = merge(*configs)
127 if errors := list(merged_configs.validate()):
--> 128 raise ValidationErrors(errors)
130 return merged_configs.cast()
ValidationErrors:
default:config_int:Value must be a positive number.