From 54a3c090a2388d3c23cd7b18a81f61a3ea52ad49 Mon Sep 17 00:00:00 2001 From: Peder Hovdan Andresen <107681714+pederhan@users.noreply.github.com> Date: Mon, 10 Jun 2024 15:37:06 +0200 Subject: [PATCH] Instantiate JSON mapping validator at top level (#260) --- mreg_cli/utilities/api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mreg_cli/utilities/api.py b/mreg_cli/utilities/api.py index 19eca2f4..9ebad230 100644 --- a/mreg_cli/utilities/api.py +++ b/mreg_cli/utilities/api.py @@ -48,6 +48,8 @@ T = TypeVar("T") +JsonMappingValidator = TypeAdapter(JsonMapping) + def error(msg: str | Exception, code: int = os.EX_UNAVAILABLE) -> NoReturn: """Print an error message and exits with the given code.""" @@ -362,10 +364,8 @@ def get_list_unique( ret = get_list_generic(path, params, ok404, expect_one_result=True) if not ret: return None - try: - validator = TypeAdapter(JsonMapping) - return validator.validate_python(ret) + return JsonMappingValidator.validate_python(ret) except ValueError as e: raise ValidationError(f"Failed to validate response from {path}: {e}") from e