I'd like to dynamically create a Pydantic model from a dataclass, similar to how you can dynamically create a Marshmallow schema from a dataclass as in marshmallow-dataclass or https://stevenloria.com/dynamic-schemas-in-marshmallow/. Is there already a library or easy way to do this?
Some background - I prefer using a dataclass in my business logic rather than using the Pydantic model directly. I use the Pydantic model for serializing/deserializing data with camel-cased fields within my FastAPI app only. However, I find myself basically duplicating the dataclass definition which isn't efficient.
Sample Input:
from typing import List
from dataclasses import dataclass
@dataclass
class Item:
id: int = None
stuff: str = None
height: float = None
@dataclass
class Bag:
id: int = None
name: str = None
things: List[Item] = None
@dataclass
class Basket:
id: int = None
recipient: str = None
bags: List[Bag] = None
best_item: Item = None
Desired output:
from typing import List
from pydantic.main import BaseModel
def camel_case_converter(value: str):
parts = value.lower().split('_')
return parts[0] + ''.join(i.title() for i in parts[1:])
class CamelBaseModel(BaseModel):
class Config:
alias_generator = camel_case_converter
class Item(CamelBaseModel):
id: int = None
stuff: str = None
height: float = None
class Bag(CamelBaseModel):
id: int = None
name: str = None
things: List[Item] = None
class Basket(CamelBaseModel):
id: int = None
recipient: str = None
bags: List[Bag] = None
best_item: Item = None