Feature/adds const #30
@@ -9,6 +9,11 @@ from typing_extensions import Annotated, Any, Unpack
|
|||||||
class ConstTypeParser(GenericTypeParser):
|
class ConstTypeParser(GenericTypeParser):
|
||||||
json_schema_type = "const"
|
json_schema_type = "const"
|
||||||
|
|
||||||
|
default_mappings = {
|
||||||
|
"const": "default",
|
||||||
|
"description": "description",
|
||||||
|
}
|
||||||
|
|
||||||
def from_properties_impl(
|
def from_properties_impl(
|
||||||
self, name, properties, **kwargs: Unpack[TypeParserOptions]
|
self, name, properties, **kwargs: Unpack[TypeParserOptions]
|
||||||
):
|
):
|
||||||
@@ -23,10 +28,7 @@ class ConstTypeParser(GenericTypeParser):
|
|||||||
)
|
)
|
||||||
|
|
||||||
const_type = self._build_const_type(const_value)
|
const_type = self._build_const_type(const_value)
|
||||||
parsed_properties = {
|
parsed_properties = self.mappings_properties_builder(properties, **kwargs)
|
||||||
"default": const_value,
|
|
||||||
"description": properties.get("description"),
|
|
||||||
}
|
|
||||||
|
|
||||||
return const_type, parsed_properties
|
return const_type, parsed_properties
|
||||||
|
|
||||||
@@ -38,9 +40,4 @@ class ConstTypeParser(GenericTypeParser):
|
|||||||
)
|
)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
return Annotated[
|
return Annotated[type(const_value), AfterValidator(_validate_const_value)]
|
||||||
type(const_value),
|
|
||||||
AfterValidator(
|
|
||||||
_validate_const_value
|
|
||||||
)
|
|
||||||
]
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from jambo.parser._type_parser import GenericTypeParser
|
from jambo.parser._type_parser import GenericTypeParser
|
||||||
from jambo.types.type_parser_options import TypeParserOptions
|
from jambo.types.type_parser_options import TypeParserOptions
|
||||||
|
|
||||||
from pydantic import BaseModel, Field, create_model
|
from pydantic import BaseModel, ConfigDict, Field, create_model
|
||||||
from typing_extensions import Any, Unpack
|
from typing_extensions import Any, Unpack
|
||||||
|
|
||||||
|
|
||||||
@@ -43,8 +43,10 @@ class ObjectTypeParser(GenericTypeParser):
|
|||||||
:param required_keys: List of required keys in the schema.
|
:param required_keys: List of required keys in the schema.
|
||||||
:return: A Pydantic model class.
|
:return: A Pydantic model class.
|
||||||
"""
|
"""
|
||||||
|
model_config = ConfigDict(validate_assignment=True)
|
||||||
fields = cls._parse_properties(schema, required_keys, **kwargs)
|
fields = cls._parse_properties(schema, required_keys, **kwargs)
|
||||||
return create_model(name, **fields)
|
|
||||||
|
return create_model(name, __config__=model_config, **fields)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _parse_properties(
|
def _parse_properties(
|
||||||
|
|||||||
@@ -1,23 +1,49 @@
|
|||||||
from typing_extensions import Annotated, get_args, get_origin
|
|
||||||
from webbrowser import get
|
|
||||||
from jambo.parser import ConstTypeParser
|
from jambo.parser import ConstTypeParser
|
||||||
|
|
||||||
|
from typing_extensions import Annotated, get_args, get_origin
|
||||||
|
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
|
||||||
|
|
||||||
class TestConstTypeParser(TestCase):
|
class TestConstTypeParser(TestCase):
|
||||||
def test_parse_const_type(self):
|
def test_const_type_parser(self):
|
||||||
parser = ConstTypeParser()
|
parser = ConstTypeParser()
|
||||||
|
|
||||||
expected_const_value = "United States of America"
|
expected_const_value = "United States of America"
|
||||||
properties = {
|
properties = {"const": expected_const_value}
|
||||||
"const": expected_const_value
|
|
||||||
}
|
|
||||||
|
|
||||||
parsed_type, parsed_properties = parser.from_properties(
|
parsed_type, parsed_properties = parser.from_properties_impl(
|
||||||
"country", properties
|
"country", properties
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertEqual(get_origin(parsed_type), Annotated)
|
self.assertEqual(get_origin(parsed_type), Annotated)
|
||||||
|
|
||||||
self.assertIn(str, get_args(parsed_type))
|
self.assertIn(str, get_args(parsed_type))
|
||||||
|
|
||||||
|
self.assertEqual(parsed_properties["default"], expected_const_value)
|
||||||
|
|
||||||
|
def test_const_type_parser_invalid_properties(self):
|
||||||
|
parser = ConstTypeParser()
|
||||||
|
|
||||||
|
expected_const_value = "United States of America"
|
||||||
|
properties = {"notConst": expected_const_value}
|
||||||
|
|
||||||
|
with self.assertRaises(ValueError) as context:
|
||||||
|
parser.from_properties_impl("invalid_country", properties)
|
||||||
|
|
||||||
|
self.assertIn(
|
||||||
|
"Const type invalid_country must have 'const' property defined",
|
||||||
|
str(context.exception),
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_const_type_parser_invalid_const_value(self):
|
||||||
|
parser = ConstTypeParser()
|
||||||
|
|
||||||
|
properties = {"const": {}}
|
||||||
|
|
||||||
|
with self.assertRaises(ValueError) as context:
|
||||||
|
parser.from_properties_impl("invalid_country", properties)
|
||||||
|
|
||||||
|
self.assertIn(
|
||||||
|
"Const type invalid_country must have 'const' value of allowed types",
|
||||||
|
str(context.exception),
|
||||||
|
)
|
||||||
|
|||||||
@@ -634,3 +634,26 @@ class TestSchemaConverter(TestCase):
|
|||||||
|
|
||||||
obj = Model()
|
obj = Model()
|
||||||
self.assertEqual(obj.status.value, "active")
|
self.assertEqual(obj.status.value, "active")
|
||||||
|
|
||||||
|
def test_const_type_parser(self):
|
||||||
|
schema = {
|
||||||
|
"title": "Country",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"name": {
|
||||||
|
"const": "United States of America",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["name"],
|
||||||
|
}
|
||||||
|
|
||||||
|
Model = SchemaConverter.build(schema)
|
||||||
|
|
||||||
|
obj = Model()
|
||||||
|
self.assertEqual(obj.name, "United States of America")
|
||||||
|
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
obj.name = "Canada"
|
||||||
|
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
Model(name="Canada")
|
||||||
|
|||||||
Reference in New Issue
Block a user