Skip to content

Commit

Permalink
Merge pull request #121 from argyle-engineering/docs
Browse files Browse the repository at this point in the history
Add some docs
  • Loading branch information
povilasb committed Jul 2, 2024
2 parents 0e66ac2 + efcc811 commit 7eabfca
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Transfer your type safe Python data models to TypeScript:

![example](example.gif)

See the [docs](docs.md).

## Similar projects

* https://github.com/cs-cordero/py-ts-interfaces
67 changes: 67 additions & 0 deletions docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# pydantic2zod

Translates [pydantic](pydantic-docs.helpmanual.io/) -> [zod](zod.dev/) declarations.

## Installation

```sh
pip install git+https://github.com/argyle-engineering/pydantic2zod
```

## CLI

The compiler must be run within the same Python environment as your project:
```sh
$ cd my-project
$ poetry run python -m pydantic2zod my_project.models
```

## As a library

Translating **pydantic** declarations to **zod** out ouf the box may not work for
more sophisticated cases. Then you can use `pydantic2zod` as a library to implement
your own specific compiler:
```py
# my_project/scripts/pydantic_to_zod.py

from pydantic2zod import Compiler

class Compiler(pydantic2zod.Compiler)
...

ts_src = Compiler().parse("examples.eshop").to_zod()
print(ts_src)
```

Now lets say we want to omit some models as they may not be relative in your TypeScript code:
```py
class Compiler(pydantic2zod.Compiler):
IGNORE_TYPES = {"examples.eshop.Order"}
```

Or we can rename others:
```py
class Compiler(pydantic2zod.Compiler):
MODEL_RENAME_RULES = {"examples.eshop.Product": "Item"}
```

We can also manually edit the models and individual fields:
```py
class Compiler(pydantic2zod.Compiler):
MODEL_RENAME_RULES = {"examples.eshop.Product": "Item"}

def _modify_models(self, pydantic_models: list[ClassDecl]) -> list[ClassDecl]:
for model in pydantic_models:
if model.name == "Item":
for f in model.fields:
# In pydantic declarations Product.description is optional.
# Lets make it required in zod.
if f.name == "description":
f.type = BuiltinType(name="str")

return pydantic_models
```

We could even generate new models on the fly this way.

See a more complete example at `examples/compiler_scripting.py`.
26 changes: 26 additions & 0 deletions examples/compiler_scripting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import logging

import pydantic2zod
from pydantic2zod.model import BuiltinType, ClassDecl

logging.basicConfig(level=logging.INFO)


class Compiler(pydantic2zod.Compiler):
IGNORE_TYPES = {"examples.eshop.Order"}
MODEL_RENAME_RULES = {"examples.eshop.Product": "Item"}

def _modify_models(self, pydantic_models: list[ClassDecl]) -> list[ClassDecl]:
for model in pydantic_models:
if model.name == "Item":
for f in model.fields:
# In pydantic declarations Product.description is optional.
# Lets make it required in zod.
if f.name == "description":
f.type = BuiltinType(name="str")

return pydantic_models


ts_src = Compiler().parse("examples.eshop").to_zod()
print(ts_src)

0 comments on commit 7eabfca

Please sign in to comment.