Final Implementation of Validation Options
This commit is contained in:
@@ -2,6 +2,10 @@ from jambo.parser._type_parser import GenericTypeParser
|
||||
|
||||
from typing import TypeVar
|
||||
|
||||
from jambo.utils.properties_builder.mappings_properties_builder import (
|
||||
mappings_properties_builder,
|
||||
)
|
||||
|
||||
V = TypeVar("V")
|
||||
|
||||
|
||||
@@ -16,4 +20,10 @@ class ArrayTypeParser(GenericTypeParser):
|
||||
properties["items"]["type"]
|
||||
).from_properties(name, properties["items"])
|
||||
|
||||
return list[_item_type], {}
|
||||
_mappings = {
|
||||
"maxItems": "max_items",
|
||||
"minItems": "min_items",
|
||||
"uniqueItems": "unique_items",
|
||||
}
|
||||
|
||||
return list[_item_type], mappings_properties_builder(properties, _mappings)
|
||||
|
||||
@@ -8,4 +8,4 @@ class BooleanTypeParser(GenericTypeParser):
|
||||
|
||||
@staticmethod
|
||||
def from_properties(name, properties):
|
||||
return bool, {}
|
||||
return bool, {} # The second argument is not used in this case
|
||||
|
||||
@@ -10,5 +10,7 @@ class ObjectTypeParser(GenericTypeParser):
|
||||
def from_properties(name, properties):
|
||||
from jambo.schema_converter import SchemaConverter
|
||||
|
||||
_type = SchemaConverter.build_object(name, properties)
|
||||
return _type, {}
|
||||
return (
|
||||
SchemaConverter.build_object(name, properties),
|
||||
{}, # The second argument is not used in this case
|
||||
)
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
from jambo.parser._type_parser import GenericTypeParser
|
||||
from jambo.utils.properties_builder.mappings_properties_builder import (
|
||||
mappings_properties_builder,
|
||||
)
|
||||
|
||||
|
||||
class StringTypeParser(GenericTypeParser):
|
||||
@@ -14,8 +17,4 @@ class StringTypeParser(GenericTypeParser):
|
||||
"pattern": "pattern",
|
||||
}
|
||||
|
||||
return str, {
|
||||
_mappings[key]: value
|
||||
for key, value in properties.items()
|
||||
if key in _mappings
|
||||
}
|
||||
return str, mappings_properties_builder(properties, _mappings)
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
def mappings_properties_builder(properties, mappings):
|
||||
return {
|
||||
mappings[key]: value for key, value in properties.items() if key in mappings
|
||||
}
|
||||
@@ -1,3 +1,8 @@
|
||||
from jambo.utils.properties_builder.mappings_properties_builder import (
|
||||
mappings_properties_builder,
|
||||
)
|
||||
|
||||
|
||||
def numeric_properties_builder(properties):
|
||||
_mappings = {
|
||||
"minimum": "ge",
|
||||
@@ -7,8 +12,4 @@ def numeric_properties_builder(properties):
|
||||
"multipleOf": "multiple_of",
|
||||
}
|
||||
|
||||
return {
|
||||
_mappings[key]: value
|
||||
for key, value in properties.items()
|
||||
if key in _mappings
|
||||
}
|
||||
return mappings_properties_builder(properties, _mappings)
|
||||
|
||||
@@ -23,14 +23,15 @@ class TestTypeParser(unittest.TestCase):
|
||||
parser = IntTypeParser()
|
||||
|
||||
type_parsing, type_validator = parser.from_properties(
|
||||
"placeholder", {
|
||||
"placeholder",
|
||||
{
|
||||
"type": "integer",
|
||||
"minimum": 0,
|
||||
"exclusiveMinimum": 1,
|
||||
"maximum": 10,
|
||||
"exclusiveMaximum": 11,
|
||||
"multipleOf": 2,
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
self.assertEqual(type_parsing, int)
|
||||
@@ -44,14 +45,15 @@ class TestTypeParser(unittest.TestCase):
|
||||
parser = FloatTypeParser()
|
||||
|
||||
type_parsing, type_validator = parser.from_properties(
|
||||
"placeholder", {
|
||||
"placeholder",
|
||||
{
|
||||
"type": "number",
|
||||
"minimum": 0,
|
||||
"exclusiveMinimum": 1,
|
||||
"maximum": 10,
|
||||
"exclusiveMaximum": 11,
|
||||
"multipleOf": 2,
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
self.assertEqual(type_parsing, float)
|
||||
@@ -65,12 +67,13 @@ class TestTypeParser(unittest.TestCase):
|
||||
parser = StringTypeParser()
|
||||
|
||||
type_parsing, type_validator = parser.from_properties(
|
||||
"placeholder", {
|
||||
"placeholder",
|
||||
{
|
||||
"type": "string",
|
||||
"maxLength": 10,
|
||||
"minLength": 1,
|
||||
"pattern": "[a-zA-Z0-9]",
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
self.assertEqual(type_parsing, str)
|
||||
@@ -110,19 +113,27 @@ class TestTypeParser(unittest.TestCase):
|
||||
parser = ArrayTypeParser()
|
||||
|
||||
properties = {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {"type": "string"},
|
||||
"age": {"type": "integer"},
|
||||
},
|
||||
}
|
||||
},
|
||||
"maxItems": 10,
|
||||
"minItems": 1,
|
||||
"uniqueItems": True,
|
||||
}
|
||||
|
||||
_type, _args = parser.from_properties("placeholder", properties)
|
||||
type_parsing, type_validator = parser.from_properties("placeholder", properties)
|
||||
|
||||
Model = get_args(_type)[0]
|
||||
Model = get_args(type_parsing)[0]
|
||||
obj = Model(name="name", age=10)
|
||||
|
||||
self.assertEqual(obj.name, "name")
|
||||
self.assertEqual(obj.age, 10)
|
||||
|
||||
self.assertEqual(type_validator["max_items"], 10)
|
||||
self.assertEqual(type_validator["min_items"], 1)
|
||||
self.assertEqual(type_validator["unique_items"], True)
|
||||
|
||||
Reference in New Issue
Block a user