Initial Fields Validators Implementation
This commit is contained in:
@@ -9,17 +9,23 @@ from typing import Type
|
||||
|
||||
|
||||
class SchemaConverter:
|
||||
@staticmethod
|
||||
def build(schema):
|
||||
try:
|
||||
Validator.check_schema(schema)
|
||||
except SchemaError as e:
|
||||
raise ValueError(f"Invalid JSON Schema: {e}")
|
||||
"""
|
||||
Converts JSON Schema to Pydantic models.
|
||||
|
||||
if schema["type"] != "object":
|
||||
raise TypeError(
|
||||
f"Invalid JSON Schema: {schema['type']}. Only 'object' can be converted to Pydantic models."
|
||||
)
|
||||
This class is responsible for converting JSON Schema definitions into Pydantic models.
|
||||
It validates the schema and generates the corresponding Pydantic model with appropriate
|
||||
fields and types. The generated model can be used for data validation and serialization.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def build(schema: dict) -> Type:
|
||||
"""
|
||||
Converts a JSON Schema to a Pydantic model.
|
||||
:param schema: The JSON Schema to convert.
|
||||
:return: A Pydantic model class.
|
||||
"""
|
||||
if "title" not in schema:
|
||||
raise ValueError("JSON Schema must have a title.")
|
||||
|
||||
return SchemaConverter.build_object(schema["title"], schema)
|
||||
|
||||
@@ -27,7 +33,19 @@ class SchemaConverter:
|
||||
def build_object(
|
||||
name: str,
|
||||
schema: dict,
|
||||
):
|
||||
) -> Type:
|
||||
"""
|
||||
Converts a JSON Schema object to a Pydantic model given a name.
|
||||
:param name:
|
||||
:param schema:
|
||||
:return:
|
||||
"""
|
||||
|
||||
try:
|
||||
Validator.check_schema(schema)
|
||||
except SchemaError as e:
|
||||
raise ValueError(f"Invalid JSON Schema: {e}")
|
||||
|
||||
if schema["type"] != "object":
|
||||
raise TypeError(
|
||||
f"Invalid JSON Schema: {schema['type']}. Only 'object' can be converted to Pydantic models."
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Generic, Self, TypeVar
|
||||
|
||||
from pydantic import Field
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
@@ -17,7 +19,7 @@ class GenericTypeParser(ABC, Generic[T]):
|
||||
@abstractmethod
|
||||
def from_properties(
|
||||
name: str, properties: dict[str, any]
|
||||
) -> tuple[type[T], dict[str, any]]: ...
|
||||
) -> tuple[type[T], Field]: ...
|
||||
|
||||
@classmethod
|
||||
def get_impl(cls, type_name: str) -> Self:
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
from dataclasses import Field
|
||||
|
||||
from jambo.types._type_parser import GenericTypeParser
|
||||
|
||||
|
||||
@@ -8,4 +10,15 @@ class IntTypeParser(GenericTypeParser):
|
||||
|
||||
@staticmethod
|
||||
def from_properties(name, properties):
|
||||
return int, {}
|
||||
_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)
|
||||
|
||||
Reference in New Issue
Block a user