1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00

29 lines
771 B
Python
Raw Normal View History

2020-11-02 23:08:07 -07:00
"""Post-Validation Validation.
Some validations need to occur across multiple config files.
"""
# Standard Library
from typing import Any, Dict, List, Union, TypeVar
2020-11-02 23:08:07 -07:00
# Third Party
from pydantic import ValidationError
# Project
from hyperglass.exceptions.private import ConfigInvalid
2020-11-02 23:08:07 -07:00
Importer = TypeVar("Importer")
2020-11-02 23:08:07 -07:00
2021-09-12 15:09:24 -07:00
def validate_config(config: Union[Dict[str, Any], List[Any]], importer: Importer) -> Importer:
2020-11-02 23:08:07 -07:00
"""Validate a config dict against a model."""
validated = None
try:
if isinstance(config, Dict):
validated = importer(**config)
elif isinstance(config, List):
validated = importer(config)
except ValidationError as err:
raise ConfigInvalid(errors=err.errors()) from None
2020-11-02 23:08:07 -07:00
return validated