From 422cc2efe00c32c55d4d7a79e4a68d0af30f70b2 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Fri, 5 Dec 2025 20:15:08 -0300 Subject: [PATCH] feat: captures any str validation exception and converts it into ValidationError converts any exception thrown in the str parser example validation into ValidationError so that the user knows that this is a error in the schema and not a parsing validation exception --- jambo/parser/string_type_parser.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/jambo/parser/string_type_parser.py b/jambo/parser/string_type_parser.py index 97f3886..b09cf20 100644 --- a/jambo/parser/string_type_parser.py +++ b/jambo/parser/string_type_parser.py @@ -2,7 +2,7 @@ from jambo.exceptions import InvalidSchemaException from jambo.parser._type_parser import GenericTypeParser from jambo.types.type_parser_options import TypeParserOptions -from pydantic import AnyUrl, EmailStr, TypeAdapter +from pydantic import AnyUrl, EmailStr, TypeAdapter, ValidationError from typing_extensions import Unpack from datetime import date, datetime, time, timedelta @@ -62,11 +62,16 @@ class StringTypeParser(GenericTypeParser): if format_type in self.format_pattern_mapping: mapped_properties["pattern"] = self.format_pattern_mapping[format_type] - if "examples" in mapped_properties: - mapped_properties["examples"] = [ - TypeAdapter(mapped_type).validate_python(example) - for example in mapped_properties["examples"] - ] + try: + if "examples" in mapped_properties: + mapped_properties["examples"] = [ + TypeAdapter(mapped_type).validate_python(example) + for example in mapped_properties["examples"] + ] + except ValidationError as err: + raise InvalidSchemaException( + f"Invalid example type for field {name}." + ) from err if "json_schema_extra" not in mapped_properties: mapped_properties["json_schema_extra"] = {}