omry / omegaconf

Flexible Python configuration system. The last one you will ever need.
BSD 3-Clause "New" or "Revised" License
1.94k stars 105 forks source link

Is there a way to display all the ValidationErrors on merging? #1050

Closed aksajja closed 1 year ago

aksajja commented 1 year ago

OmegaConf.merge() is a way to merge a schema that is known to be valid with a schema that needs to be validated. There is a lot of time lost in fixing one issue after another when several ValidationErrors (or others) exist. If there is no immediate way to do this, is there a workaround or a conversation about having such a feature?

Jasha10 commented 1 year ago

Hi @aksajja, No, there's no way to display multiple validation errors at once. OmegaConf validates the schema in a recursive way, walking down the tree of the config. If it encounters a validation error during this recursive process, it immediately raises an exception which prevents later validation errors from being discovered.

is there a workaround or a conversation about having such a feature?

One idea could be make your config more granular: instead of merging one big config into a schema, you could merge a bunch of smaller configs into a schema

Instead of this:

cfg = OmegaConf.merge(schema, big_config)

You could break up big_config into parts like this:

cfg = OmegaConf.merge(schema, part1)
cfg = OmegaConf.merge(cfg, part2)
cfg = OmegaConf.merge(cfg, part3)
cfg = OmegaConf.merge(cfg, part4)

You could then get an approximation of the errors that would result from your merge by trying to merge each part into the schema directly:

# print err messages that might result from merge:
try:
    OmegaConf.merge(schema, part1)
except ValidationError as exc:
    print(f"Got validation error when merging part1: {exc}")

try:
    OmegaConf.merge(schema, part2)
except ValidationError as exc:
    print(f"Got validation error when merging part2: {exc}")

try:
    OmegaConf.merge(schema, part3)
except ValidationError as exc:
    print(f"Got validation error when merging part3: {exc}")

try:
    OmegaConf.merge(schema, part4)
except ValidationError as exc:
    print(f"Got validation error when merging part4: {exc}")
aksajja commented 1 year ago

Thanks for the detailed answer. I'm closing this issue since the primary questions have been answered. For my project, I intend to develop a solution where all ValidationErrors (or whatever errors we can bundle into this) will be caught. This can be done by recursively running the merge operation while filling in correct default values until the merge succeeds. The concatenated list of all the errors can then be displayed to the user.

Do you think I should create a feature request and send out a PR? Please let me know if such a feature has intentionally been avoided.