Feature/explicit exception type #47
@@ -1,3 +1,4 @@
|
|||||||
|
from jambo.exceptions import InvalidSchemaException
|
||||||
from jambo.types.type_parser_options import JSONSchema, TypeParserOptions
|
from jambo.types.type_parser_options import JSONSchema, TypeParserOptions
|
||||||
|
|
||||||
from pydantic import Field, TypeAdapter
|
from pydantic import Field, TypeAdapter
|
||||||
@@ -46,8 +47,8 @@ class GenericTypeParser(ABC, Generic[T]):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if not self._validate_default(parsed_type, parsed_properties):
|
if not self._validate_default(parsed_type, parsed_properties):
|
||||||
raise ValueError(
|
raise InvalidSchemaException(
|
||||||
f"Default value {properties.get('default')} is not valid for type {parsed_type.__name__}"
|
"Default value is not valid", invalid_field=name
|
||||||
)
|
)
|
||||||
|
|
||||||
return parsed_type, parsed_properties
|
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
|
if schema_value is None or schema_value == properties[schema_type]: # type: ignore
|
||||||
return subcls
|
return subcls
|
||||||
|
|
||||||
raise ValueError("Unknown type")
|
raise InvalidSchemaException(
|
||||||
|
"No suitable type parser found", invalid_field=str(properties)
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _get_schema_type(cls) -> tuple[str, str | None]:
|
def _get_schema_type(cls) -> tuple[str, str | None]:
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from jambo.exceptions import InvalidSchemaException
|
||||||
from jambo.parser._type_parser import GenericTypeParser
|
from jambo.parser._type_parser import GenericTypeParser
|
||||||
from jambo.types.json_schema_type import JSONSchema
|
from jambo.types.json_schema_type import JSONSchema
|
||||||
from jambo.types.type_parser_options import TypeParserOptions
|
from jambo.types.type_parser_options import TypeParserOptions
|
||||||
@@ -33,13 +34,18 @@ class AllOfTypeParser(GenericTypeParser):
|
|||||||
sub_properties: list[JSONSchema],
|
sub_properties: list[JSONSchema],
|
||||||
) -> type[GenericTypeParser]:
|
) -> type[GenericTypeParser]:
|
||||||
if not sub_properties:
|
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(
|
parsers: set[type[GenericTypeParser]] = set(
|
||||||
GenericTypeParser._get_impl(sub_property) for sub_property in sub_properties
|
GenericTypeParser._get_impl(sub_property) for sub_property in sub_properties
|
||||||
)
|
)
|
||||||
if len(parsers) != 1:
|
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()
|
return parsers.pop()
|
||||||
|
|
||||||
@@ -68,8 +74,8 @@ class AllOfTypeParser(GenericTypeParser):
|
|||||||
|
|
||||||
if prop_name == "default":
|
if prop_name == "default":
|
||||||
if old_value != new_value:
|
if old_value != new_value:
|
||||||
raise ValueError(
|
raise InvalidSchemaException(
|
||||||
f"Invalid JSON Schema: conflicting defaults for '{prop_name}'"
|
f"Conflicting defaults for '{prop_name}'", invalid_field=prop_name
|
||||||
)
|
)
|
||||||
return old_value
|
return old_value
|
||||||
|
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ class TestSchemaConverter(TestCase):
|
|||||||
"type": "string",
|
"type": "string",
|
||||||
}
|
}
|
||||||
|
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(ValueError):
|
||||||
SchemaConverter.build(schema)
|
SchemaConverter.build(schema)
|
||||||
|
|
||||||
def test_is_invalid_field(self):
|
def test_is_invalid_field(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user