Feature/explicit exception type #47

Merged
HideyoshiNakazone merged 6 commits from feature/explicit-exception-type into main 2025-09-14 04:13:28 +00:00
3 changed files with 17 additions and 8 deletions
Showing only changes of commit f4d84d2749 - Show all commits

View File

@@ -1,3 +1,4 @@
from jambo.exceptions import InvalidSchemaException
from jambo.types.type_parser_options import JSONSchema, TypeParserOptions
from pydantic import Field, TypeAdapter
@@ -46,8 +47,8 @@ class GenericTypeParser(ABC, Generic[T]):
)
if not self._validate_default(parsed_type, parsed_properties):
raise ValueError(
f"Default value {properties.get('default')} is not valid for type {parsed_type.__name__}"
raise InvalidSchemaException(
"Default value is not valid", invalid_field=name
)
return parsed_type, parsed_properties
@@ -79,7 +80,9 @@ class GenericTypeParser(ABC, Generic[T]):
if schema_value is None or schema_value == properties[schema_type]: # type: ignore
return subcls
raise ValueError("Unknown type")
raise InvalidSchemaException(
"No suitable type parser found", invalid_field=str(properties)
)
@classmethod
def _get_schema_type(cls) -> tuple[str, str | None]:

View File

@@ -1,3 +1,4 @@
from jambo.exceptions import InvalidSchemaException
from jambo.parser._type_parser import GenericTypeParser
from jambo.types.json_schema_type import JSONSchema
from jambo.types.type_parser_options import TypeParserOptions
@@ -33,13 +34,18 @@ class AllOfTypeParser(GenericTypeParser):
sub_properties: list[JSONSchema],
) -> type[GenericTypeParser]:
if not sub_properties:
raise ValueError("Invalid JSON Schema: 'allOf' is empty.")
raise InvalidSchemaException(
"'allOf' must contain at least one schema", invalid_field="allOf"
)
parsers: set[type[GenericTypeParser]] = set(
GenericTypeParser._get_impl(sub_property) for sub_property in sub_properties
)
if len(parsers) != 1:
raise ValueError("Invalid JSON Schema: allOf types do not match.")
raise InvalidSchemaException(
"All sub-schemas in 'allOf' must resolve to the same type",
invalid_field="allOf",
)
return parsers.pop()
@@ -68,8 +74,8 @@ class AllOfTypeParser(GenericTypeParser):
if prop_name == "default":
if old_value != new_value:
raise ValueError(
f"Invalid JSON Schema: conflicting defaults for '{prop_name}'"
raise InvalidSchemaException(
f"Conflicting defaults for '{prop_name}'", invalid_field=prop_name
)
return old_value

View File

@@ -60,7 +60,7 @@ class TestSchemaConverter(TestCase):
"type": "string",
}
with self.assertRaises(TypeError):
with self.assertRaises(ValueError):
SchemaConverter.build(schema)
def test_is_invalid_field(self):