Implements Int, Float, String Validators

This commit is contained in:
2025-04-08 00:18:24 -03:00
parent 936c5b350a
commit 9e1763c35a
17 changed files with 110 additions and 57 deletions

View File

@@ -1,4 +1,4 @@
from jambo.types._type_parser import GenericTypeParser from jambo.parser._type_parser import GenericTypeParser
from typing import TypeVar from typing import TypeVar

View File

@@ -1,4 +1,4 @@
from jambo.types._type_parser import GenericTypeParser from jambo.parser._type_parser import GenericTypeParser
class BooleanTypeParser(GenericTypeParser): class BooleanTypeParser(GenericTypeParser):

View File

@@ -0,0 +1,12 @@
from jambo.parser._type_parser import GenericTypeParser
from jambo.utils.properties_builder.numeric_properties_builder import numeric_properties_builder
class FloatTypeParser(GenericTypeParser):
mapped_type = float
json_schema_type = "number"
@staticmethod
def from_properties(name, properties):
return float, numeric_properties_builder(properties)

View File

@@ -0,0 +1,12 @@
from jambo.parser._type_parser import GenericTypeParser
from jambo.utils.properties_builder.numeric_properties_builder import numeric_properties_builder
class IntTypeParser(GenericTypeParser):
mapped_type = int
json_schema_type = "integer"
@staticmethod
def from_properties(name, properties):
return int, numeric_properties_builder(properties)

View File

@@ -1,4 +1,4 @@
from jambo.types._type_parser import GenericTypeParser from jambo.parser._type_parser import GenericTypeParser
class ObjectTypeParser(GenericTypeParser): class ObjectTypeParser(GenericTypeParser):

View File

@@ -0,0 +1,21 @@
from jambo.parser._type_parser import GenericTypeParser
class StringTypeParser(GenericTypeParser):
mapped_type = str
json_schema_type = "string"
@staticmethod
def from_properties(name, properties):
_mappings = {
"maxLength": "max_length",
"minLength": "min_length",
"pattern": "pattern",
}
return str, {
_mappings[key]: value
for key, value in properties.items()
if key in _mappings
}

View File

@@ -1,4 +1,4 @@
from jambo.types import GenericTypeParser from jambo.parser import GenericTypeParser
from jsonschema.exceptions import SchemaError from jsonschema.exceptions import SchemaError
from jsonschema.protocols import Validator from jsonschema.protocols import Validator
@@ -78,7 +78,7 @@ class SchemaConverter:
@staticmethod @staticmethod
def _build_field( def _build_field(
name, properties: dict, required_keys: list[str] name, properties: dict, required_keys: list[str]
) -> tuple[type, Field]: ) -> tuple[type, dict]:
_field_type, _field_args = GenericTypeParser.get_impl( _field_type, _field_args = GenericTypeParser.get_impl(
properties["type"] properties["type"]
).from_properties(name, properties) ).from_properties(name, properties)

View File

@@ -1,11 +0,0 @@
from jambo.types._type_parser import GenericTypeParser
class FloatTypeParser(GenericTypeParser):
mapped_type = float
json_schema_type = "number"
@staticmethod
def from_properties(name, properties):
return float, {}

View File

@@ -1,24 +0,0 @@
from dataclasses import Field
from jambo.types._type_parser import GenericTypeParser
class IntTypeParser(GenericTypeParser):
mapped_type = int
json_schema_type = "integer"
@staticmethod
def from_properties(name, properties):
_field_properties = dict()
if "minimum" in properties:
_field_properties["ge"] = properties["minimum"]
if "maximum" in properties:
_field_properties["le"] = properties["maximum"]
if "multipleOf" in properties:
_field_properties["multiple_of"] = properties["multipleOf"]
return int, Field(**_field_properties)

View File

@@ -1,11 +0,0 @@
from jambo.types._type_parser import GenericTypeParser
class StringTypeParser(GenericTypeParser):
mapped_type = str
json_schema_type = "string"
@staticmethod
def from_properties(name, properties):
return str, {}

0
jambo/utils/__init__.py Normal file
View File

View File

@@ -0,0 +1,14 @@
def numeric_properties_builder(properties):
_mappings = {
"minimum": "ge",
"exclusiveMinimum": "gt",
"maximum": "le",
"exclusiveMaximum": "lt",
"multipleOf": "multiple_of",
}
return {
_mappings[key]: value
for key, value in properties.items()
if key in _mappings
}

View File

View File

@@ -1,4 +1,4 @@
from jambo.types import ( from jambo.parser import (
ArrayTypeParser, ArrayTypeParser,
FloatTypeParser, FloatTypeParser,
GenericTypeParser, GenericTypeParser,
@@ -22,21 +22,61 @@ class TestTypeParser(unittest.TestCase):
def test_int_parser(self): def test_int_parser(self):
parser = IntTypeParser() parser = IntTypeParser()
type_parsing, type_validator = parser.from_properties("placeholder", {}) type_parsing, type_validator = parser.from_properties(
"placeholder", {
"type": "integer",
"minimum": 0,
"exclusiveMinimum": 1,
"maximum": 10,
"exclusiveMaximum": 11,
"multipleOf": 2,
}
)
self.assertEqual(type_parsing, int) self.assertEqual(type_parsing, int)
self.assertEqual(type_validator["ge"], 0)
self.assertEqual(type_validator["gt"], 1)
self.assertEqual(type_validator["le"], 10)
self.assertEqual(type_validator["lt"], 11)
self.assertEqual(type_validator["multiple_of"], 2)
def test_float_parser(self): def test_float_parser(self):
parser = FloatTypeParser() parser = FloatTypeParser()
expected_definition = (float, {})
self.assertEqual(parser.from_properties("placeholder", {}), expected_definition) type_parsing, type_validator = parser.from_properties(
"placeholder", {
"type": "number",
"minimum": 0,
"exclusiveMinimum": 1,
"maximum": 10,
"exclusiveMaximum": 11,
"multipleOf": 2,
}
)
self.assertEqual(type_parsing, float)
self.assertEqual(type_validator["ge"], 0)
self.assertEqual(type_validator["gt"], 1)
self.assertEqual(type_validator["le"], 10)
self.assertEqual(type_validator["lt"], 11)
self.assertEqual(type_validator["multiple_of"], 2)
def test_string_parser(self): def test_string_parser(self):
parser = StringTypeParser() parser = StringTypeParser()
expected_definition = (str, {})
self.assertEqual(parser.from_properties("placeholder", {}), expected_definition) type_parsing, type_validator = parser.from_properties(
"placeholder", {
"type": "string",
"maxLength": 10,
"minLength": 1,
"pattern": "[a-zA-Z0-9]",
}
)
self.assertEqual(type_parsing, str)
self.assertEqual(type_validator["max_length"], 10)
self.assertEqual(type_validator["min_length"], 1)
self.assertEqual(type_validator["pattern"], "[a-zA-Z0-9]")
def test_object_parser(self): def test_object_parser(self):
parser = ObjectTypeParser() parser = ObjectTypeParser()