feat: adds support for list of types #66
@@ -74,9 +74,7 @@ class GenericTypeParser(ABC, Generic[T]):
|
|||||||
:return: A tuple containing the type and its properties.
|
:return: A tuple containing the type and its properties.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
parser = cls._get_impl(
|
parser = cls._get_impl(cls._normalize_properties(properties))
|
||||||
cls._normalize_properties(properties)
|
|
||||||
)
|
|
||||||
|
|
||||||
return parser().from_properties(name=name, properties=properties, **kwargs)
|
return parser().from_properties(name=name, properties=properties, **kwargs)
|
||||||
|
|
||||||
@@ -94,10 +92,10 @@ class GenericTypeParser(ABC, Generic[T]):
|
|||||||
|
|
||||||
if isinstance(type_value, list) and len(type_value) == 0:
|
if isinstance(type_value, list) and len(type_value) == 0:
|
||||||
raise InvalidSchemaException(
|
raise InvalidSchemaException(
|
||||||
"Invalid schema: 'type' list cannot be empty", invalid_field=str(properties)
|
"Invalid schema: 'type' list cannot be empty",
|
||||||
|
invalid_field=str(properties),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
if isinstance(type_value, list) and len(type_value) == 1:
|
if isinstance(type_value, list) and len(type_value) == 1:
|
||||||
properties["type"] = type_value[0]
|
properties["type"] = type_value[0]
|
||||||
return properties
|
return properties
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from jambo.exceptions import InternalAssertionException, InvalidSchemaException, UnsupportedSchemaException
|
from jambo.exceptions import InvalidSchemaException, UnsupportedSchemaException
|
||||||
from jambo.parser import ObjectTypeParser, RefTypeParser
|
from jambo.parser import ObjectTypeParser, RefTypeParser
|
||||||
from jambo.types import JSONSchema, RefCacheDict
|
from jambo.types import JSONSchema, RefCacheDict
|
||||||
|
|
||||||
@@ -139,7 +139,8 @@ class SchemaConverter:
|
|||||||
type_value = schema.get("type")
|
type_value = schema.get("type")
|
||||||
if isinstance(type_value, list):
|
if isinstance(type_value, list):
|
||||||
raise InvalidSchemaException(
|
raise InvalidSchemaException(
|
||||||
"Invalid schema: 'type' cannot be a list at the top level", invalid_field=str(schema)
|
"Invalid schema: 'type' cannot be a list at the top level",
|
||||||
|
invalid_field=str(schema),
|
||||||
)
|
)
|
||||||
|
|
||||||
return type_value
|
return type_value
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ JSONSchema = TypedDict(
|
|||||||
"description": str,
|
"description": str,
|
||||||
"default": JSONType,
|
"default": JSONType,
|
||||||
"examples": List[JSONType],
|
"examples": List[JSONType],
|
||||||
"type": JSONSchemaType|List[JSONSchemaType],
|
"type": JSONSchemaType | List[JSONSchemaType],
|
||||||
"enum": List[JSONType],
|
"enum": List[JSONType],
|
||||||
"const": JSONType,
|
"const": JSONType,
|
||||||
"properties": Dict[str, "JSONSchema"],
|
"properties": Dict[str, "JSONSchema"],
|
||||||
|
|||||||
@@ -1003,11 +1003,7 @@ class TestSchemaConverter(TestCase):
|
|||||||
schema = {
|
schema = {
|
||||||
"title": "TestListType",
|
"title": "TestListType",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {"values": {"type": ["string", "number"]}},
|
||||||
"values": {
|
|
||||||
"type": ["string", "number"]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Model = self.converter.build_with_cache(schema)
|
Model = self.converter.build_with_cache(schema)
|
||||||
@@ -1022,11 +1018,7 @@ class TestSchemaConverter(TestCase):
|
|||||||
schema = {
|
schema = {
|
||||||
"title": "TestListType",
|
"title": "TestListType",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {"values": {"type": ["string"]}},
|
||||||
"values": {
|
|
||||||
"type": ["string"]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Model = self.converter.build_with_cache(schema)
|
Model = self.converter.build_with_cache(schema)
|
||||||
@@ -1038,21 +1030,14 @@ class TestSchemaConverter(TestCase):
|
|||||||
schema = {
|
schema = {
|
||||||
"title": "TestListType",
|
"title": "TestListType",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {"values": {"type": []}},
|
||||||
"values": {
|
|
||||||
"type": []
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
with self.assertRaises(InvalidSchemaException):
|
with self.assertRaises(InvalidSchemaException):
|
||||||
self.converter.build_with_cache(schema)
|
self.converter.build_with_cache(schema)
|
||||||
|
|
||||||
def test_parse_list_type_root_level_throws(self):
|
def test_parse_list_type_root_level_throws(self):
|
||||||
schema = {
|
schema = {"title": "TestListType", "type": ["string", "number"]}
|
||||||
"title": "TestListType",
|
|
||||||
"type": ["string", "number"]
|
|
||||||
}
|
|
||||||
|
|
||||||
with self.assertRaises(InvalidSchemaException):
|
with self.assertRaises(InvalidSchemaException):
|
||||||
self.converter.build_with_cache(schema)
|
self.converter.build_with_cache(schema)
|
||||||
Reference in New Issue
Block a user