diff --git a/jambo/parser/_type_parser.py b/jambo/parser/_type_parser.py index cce8042..cb7885e 100644 --- a/jambo/parser/_type_parser.py +++ b/jambo/parser/_type_parser.py @@ -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]: diff --git a/jambo/parser/allof_type_parser.py b/jambo/parser/allof_type_parser.py index 2fb62f9..92c5ff2 100644 --- a/jambo/parser/allof_type_parser.py +++ b/jambo/parser/allof_type_parser.py @@ -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 diff --git a/tests/test_schema_converter.py b/tests/test_schema_converter.py index 6f831f2..2d4099b 100644 --- a/tests/test_schema_converter.py +++ b/tests/test_schema_converter.py @@ -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):