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

View File

@@ -1,4 +1,4 @@
from jambo.types._type_parser import GenericTypeParser
from jambo.parser._type_parser import 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):

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.protocols import Validator
@@ -78,7 +78,7 @@ class SchemaConverter:
@staticmethod
def _build_field(
name, properties: dict, required_keys: list[str]
) -> tuple[type, Field]:
) -> tuple[type, dict]:
_field_type, _field_args = GenericTypeParser.get_impl(
properties["type"]
).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