2020-11-02 23:08:07 -07:00
|
|
|
"""Post-Validation Validation.
|
|
|
|
|
|
|
|
Some validations need to occur across multiple config files.
|
|
|
|
"""
|
|
|
|
# Standard Library
|
2021-09-10 23:13:27 -07:00
|
|
|
from typing import Any, Dict, List, Union, TypeVar
|
2020-11-02 23:08:07 -07:00
|
|
|
|
|
|
|
# Third Party
|
|
|
|
from pydantic import ValidationError
|
|
|
|
|
|
|
|
# Project
|
2021-09-10 23:13:27 -07:00
|
|
|
from hyperglass.exceptions.private import ConfigInvalid
|
2020-11-02 23:08:07 -07:00
|
|
|
|
2021-09-10 23:13:27 -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:
|
2021-09-07 22:58:39 -07:00
|
|
|
raise ConfigInvalid(errors=err.errors()) from None
|
2020-11-02 23:08:07 -07:00
|
|
|
|
|
|
|
return validated
|