pydantic2zod
pydantic2zod copied to clipboard
Confusion: Extending Compiler
Hey there,
This package, at face value, may well be a lifesaver for an application im building, connecting a django backend with a typescript nextjs frontend.
I've started playing around with the library, and the default compiler, but have noticed that theres no first class support for some pydantic features.
Namely:
- I want to add a new type for date (datetime.datetime --> z.date())
- I want to extend to capture information from 'Annotated' pydantic types (which for instance, allow conforming a float to a certain range using gt / lt attributes). These would map to zods min() and max() functions
I've tried extending using a custom compiler and overriding the type for these fields, something similar to :
class Compiler(pydantic2zod.Compiler):
def _modify_models(self, pydantic_models: list[ClassDecl]) -> list[ClassDecl]:
for model in pydantic_models:
for f in model.fields:
if f.type == UserDefinedType(name='datetime.datetime'):
print(f"found datetime")
f.type = LiteralType('z.date()')
return pydantic_models
but this doesnt quite work.