-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_model.py
40 lines (28 loc) · 932 Bytes
/
data_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from abc import ABC, abstractmethod
from dataclasses import dataclass
from pydantic import BaseModel
import ujson as json
from json import JsonSchema
# from typing import Any, Union, Optional, NewType, Generic
@dataclass
class DataModel(ABC):
@abstractmethod
def as_json_schema(self) -> JsonSchema:
pass
@dataclass
class JsonSchemaDataModel(DataModel):
_jsonschema: JsonSchema
def __init__(self, schema: JsonSchema):
self._jsonschema = schema
def as_json_schema(self) -> JsonSchema:
return self._jsonschema
@dataclass
class PydanticDataModel(DataModel):
_model: BaseModel
_jsonschema: JsonSchema
def __init__(self, model: BaseModel):
self._model = model
model_dict: dict = self._model.model_json_schema(mode="validation")
self._jsonschema = JsonSchema(model_dict)
def as_json_schema(self) -> JsonSchema:
return self._jsonschema