Adds TypedDict for JsonSchema for Reference
This commit is contained in:
@@ -7,6 +7,8 @@ from pydantic.fields import Field
|
|||||||
|
|
||||||
from typing import Type
|
from typing import Type
|
||||||
|
|
||||||
|
from jambo.types.json_schema_type import JSONSchema
|
||||||
|
|
||||||
|
|
||||||
class SchemaConverter:
|
class SchemaConverter:
|
||||||
"""
|
"""
|
||||||
@@ -18,7 +20,7 @@ class SchemaConverter:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def build(schema: dict) -> Type:
|
def build(schema: JSONSchema) -> Type:
|
||||||
"""
|
"""
|
||||||
Converts a JSON Schema to a Pydantic model.
|
Converts a JSON Schema to a Pydantic model.
|
||||||
:param schema: The JSON Schema to convert.
|
:param schema: The JSON Schema to convert.
|
||||||
@@ -32,7 +34,7 @@ class SchemaConverter:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def build_object(
|
def build_object(
|
||||||
name: str,
|
name: str,
|
||||||
schema: dict,
|
schema: JSONSchema,
|
||||||
) -> Type:
|
) -> Type:
|
||||||
"""
|
"""
|
||||||
Converts a JSON Schema object to a Pydantic model given a name.
|
Converts a JSON Schema object to a Pydantic model given a name.
|
||||||
|
|||||||
80
jambo/types/json_schema_type.py
Normal file
80
jambo/types/json_schema_type.py
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
from typing import List, Dict, Union, TypedDict, Literal
|
||||||
|
|
||||||
|
|
||||||
|
JSONSchemaType = Literal[
|
||||||
|
"string", "number", "integer", "boolean", "object", "array", "null"
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
JSONType = Union[str, int, float, bool, None, Dict[str, "JSONType"], List["JSONType"]]
|
||||||
|
|
||||||
|
|
||||||
|
class JSONSchema(TypedDict, total=False):
|
||||||
|
# Basic metadata
|
||||||
|
title: str
|
||||||
|
description: str
|
||||||
|
default: JSONType
|
||||||
|
examples: List[JSONType]
|
||||||
|
|
||||||
|
# Type definitions
|
||||||
|
type: Union[JSONSchemaType, List[JSONSchemaType]]
|
||||||
|
|
||||||
|
# Object-specific keywords
|
||||||
|
properties: Dict[str, "JSONSchema"]
|
||||||
|
required: List[str]
|
||||||
|
additionalProperties: Union[bool, "JSONSchema"]
|
||||||
|
minProperties: int
|
||||||
|
maxProperties: int
|
||||||
|
patternProperties: Dict[str, "JSONSchema"]
|
||||||
|
dependencies: Dict[str, Union[List[str], "JSONSchema"]]
|
||||||
|
|
||||||
|
# Array-specific keywords
|
||||||
|
items: Union["JSONSchema", List["JSONSchema"]]
|
||||||
|
additionalItems: Union[bool, "JSONSchema"]
|
||||||
|
minItems: int
|
||||||
|
maxItems: int
|
||||||
|
uniqueItems: bool
|
||||||
|
|
||||||
|
# String-specific keywords
|
||||||
|
minLength: int
|
||||||
|
maxLength: int
|
||||||
|
pattern: str
|
||||||
|
format: str
|
||||||
|
|
||||||
|
# Number-specific keywords
|
||||||
|
minimum: float
|
||||||
|
maximum: float
|
||||||
|
exclusiveMinimum: float
|
||||||
|
exclusiveMaximum: float
|
||||||
|
multipleOf: float
|
||||||
|
|
||||||
|
# Enum and const
|
||||||
|
enum: List[JSONType]
|
||||||
|
const: JSONType
|
||||||
|
|
||||||
|
# Conditionals
|
||||||
|
if_: "JSONSchema" # 'if' is a reserved word in Python
|
||||||
|
then: "JSONSchema"
|
||||||
|
else_: "JSONSchema" # 'else' is also a reserved word
|
||||||
|
|
||||||
|
# Combination keywords
|
||||||
|
allOf: List["JSONSchema"]
|
||||||
|
anyOf: List["JSONSchema"]
|
||||||
|
oneOf: List["JSONSchema"]
|
||||||
|
not_: "JSONSchema" # 'not' is a reserved word
|
||||||
|
|
||||||
|
|
||||||
|
# Fix forward references
|
||||||
|
JSONSchema.__annotations__["properties"] = Dict[str, JSONSchema]
|
||||||
|
JSONSchema.__annotations__["items"] = Union[JSONSchema, List[JSONSchema]]
|
||||||
|
JSONSchema.__annotations__["additionalItems"] = Union[bool, JSONSchema]
|
||||||
|
JSONSchema.__annotations__["additionalProperties"] = Union[bool, JSONSchema]
|
||||||
|
JSONSchema.__annotations__["patternProperties"] = Dict[str, JSONSchema]
|
||||||
|
JSONSchema.__annotations__["dependencies"] = Dict[str, Union[List[str], JSONSchema]]
|
||||||
|
JSONSchema.__annotations__["if_"] = JSONSchema
|
||||||
|
JSONSchema.__annotations__["then"] = JSONSchema
|
||||||
|
JSONSchema.__annotations__["else_"] = JSONSchema
|
||||||
|
JSONSchema.__annotations__["allOf"] = List[JSONSchema]
|
||||||
|
JSONSchema.__annotations__["anyOf"] = List[JSONSchema]
|
||||||
|
JSONSchema.__annotations__["oneOf"] = List[JSONSchema]
|
||||||
|
JSONSchema.__annotations__["not_"] = JSONSchema
|
||||||
Reference in New Issue
Block a user