Skip to content

Commit 14aea6e

Browse files
committed
1.0.3: Example usage in readme
1 parent bbf73dd commit 14aea6e

File tree

4 files changed

+83
-16
lines changed

4 files changed

+83
-16
lines changed

README.md

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,75 @@ This tool requires that you have the lovely json2ts CLI utility installed. Instr
88
```bash
99
$ pip install pydantic-to-typescript
1010
```
11+
---
12+
### CLI
1113

12-
### Command-line usage
14+
|Prop|Description|
15+
|:----------|:-----------|
16+
|`--module`|name of the python module you would like to convert. All the pydantic models within it will be converted to typescript interfaces. Discoverable submodules will also be checked. Ex: 'pydantic2ts.examples.pydantic_models'|
17+
|`--output`|name of the file the typescript definitions should be written to. Ex: './frontend/apiTypes.ts'|
18+
|<nobr>`--json2ts-cmd`</nobr>|optional, the command used to invoke json2ts. The default is 'json2ts'. Specify this if you have it installed in a strange location and need to provide the exact path (ex: /myproject/node_modules/bin/json2ts)|
19+
---
20+
### Usage
21+
pydantic2ts/examples/pydantic_models.py:
22+
```python
23+
from pydantic import BaseModel, Extra
24+
from enum import Enum
25+
from typing import List, Dict
1326

14-
|Prop |Description|
15-
|:--------|:-----------|
16-
|`--module` |name of the python module you would like to convert. All the pydantic models within it will be converted to typescript interfaces. Discoverable submodules will also be checked. Ex: 'pydantic2ts.examples.pydantic_models'|
17-
|`--output` |name of the file the typescript definitions should be written to. Ex: '/frontend/api-types.ts'|
18-
|`--json2ts-cmd` |optional, the command used to invoke json2ts. The default is 'json2ts'. Specify this if you have it installed in a strange location and need to provide the exact path (ex: /myproject/node_modules/bin/json2ts)|
1927

20-
Example:
28+
class NoExtraProps:
29+
extra = Extra.forbid
30+
31+
32+
class Sport(str, Enum):
33+
football = 'football'
34+
basketball = 'basketball'
35+
36+
37+
class Athlete(BaseModel):
38+
name: str
39+
age: int
40+
sports: List[Sport]
41+
Config = NoExtraProps
42+
43+
44+
class Team(BaseModel):
45+
name: str
46+
sport: Sport
47+
athletes: List[Athlete]
48+
Config = NoExtraProps
49+
50+
51+
class League(BaseModel):
52+
cities: Dict[str, Team]
53+
Config = NoExtraProps
54+
```
55+
Command-line:
2156
```bash
2257
$ pydantic2ts --module pydantic2ts.examples.pydantic_models --output output.ts
2358
```
59+
output.ts:
60+
```
61+
/* tslint:disable */
62+
/**
63+
/* This file was automatically generated from pydantic models by running pydantic2ts.
64+
/* Do not modify it by hand - just update the pydantic models and then re-run the script
65+
*/
66+
67+
export interface Athlete {
68+
name: string;
69+
age: number;
70+
sports: ("football" | "basketball")[];
71+
}
72+
export interface League {
73+
cities: {
74+
[k: string]: Team;
75+
};
76+
}
77+
export interface Team {
78+
name: string;
79+
sport: "football" | "basketball";
80+
athletes: Athlete[];
81+
}
82+
```

pydantic2ts/examples/output.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
export interface Athlete {
88
name: string;
99
age: number;
10-
sports: ("football" | "basketball" | "running" | "swimming")[];
10+
sports: ("football" | "basketball")[];
11+
}
12+
export interface League {
13+
cities: {
14+
[k: string]: Team;
15+
};
1116
}
1217
export interface Team {
1318
name: string;
14-
sport: "football" | "basketball" | "running" | "swimming";
19+
sport: "football" | "basketball";
1520
athletes: Athlete[];
1621
}
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
from pydantic import BaseModel, Extra
22
from enum import Enum
3-
from typing import List
3+
from typing import List, Dict
44

55

6-
class NoUnspecifiedProps:
6+
class NoExtraProps:
77
extra = Extra.forbid
88

99

1010
class Sport(str, Enum):
1111
football = 'football'
1212
basketball = 'basketball'
13-
running = 'running'
14-
swimming = 'swimming'
1513

1614

1715
class Athlete(BaseModel):
1816
name: str
1917
age: int
2018
sports: List[Sport]
21-
Config = NoUnspecifiedProps
19+
Config = NoExtraProps
2220

2321

2422
class Team(BaseModel):
2523
name: str
2624
sport: Sport
2725
athletes: List[Athlete]
28-
Config = NoUnspecifiedProps
26+
Config = NoExtraProps
27+
28+
29+
class League(BaseModel):
30+
cities: Dict[str, Team]
31+
Config = NoExtraProps

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def readme():
2020

2121
setup(
2222
name='pydantic-to-typescript',
23-
version='1.0.2',
23+
version='1.0.3',
2424
description='Convert pydantic models to typescript interfaces',
2525
license='MIT',
2626
long_description=readme(),

0 commit comments

Comments
 (0)