pydantic2zod icon indicating copy to clipboard operation
pydantic2zod copied to clipboard

Confusion: Extending Compiler

Open apoha-pat opened this issue 1 year ago • 0 comments

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:

  1. I want to add a new type for date (datetime.datetime --> z.date())
  2. 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.

apoha-pat avatar Sep 20 '24 12:09 apoha-pat