Skip to content

Commit 6ab97df

Browse files
authored
Add ULID and Email format documentation (#2886)
1 parent 636d9e8 commit 6ab97df

2 files changed

Lines changed: 111 additions & 2 deletions

File tree

docs/supported-data-types.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ Below are the data types and features recognized by datamodel-code-generator for
2727
- datetime
2828
- time
2929
- password
30-
- email
31-
- idn-email
30+
- email (requires [`email-validator`](https://github.com/JoshData/python-email-validator))
31+
- idn-email (requires [`email-validator`](https://github.com/JoshData/python-email-validator))
3232
- path
3333
- uuid (uuid1/uuid2/uuid3/uuid4/uuid5)
34+
- ulid (requires [`python-ulid`](https://github.com/mdomke/python-ulid))
3435
- ipv4
3536
- ipv6
3637
- hostname

docs/type-mappings.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ datamodel-code-generator allows you to customize how schema types are mapped to
1313
| `--output-datetime-class` | Choose datetime output type |
1414
| `--use-pendulum` | Use Pendulum library for datetime types |
1515
| `--use-decimal-for-multiple-of` | Use Decimal for multipleOf constraints |
16+
| `format: "email"` | Email validation (requires `email-validator`) |
17+
| `format: "ulid"` | ULID type support (requires `python-ulid`) |
1618

1719
---
1820

@@ -230,6 +232,111 @@ class Event(BaseModel):
230232

231233
---
232234

235+
## Email Format Support
236+
237+
The generator supports the `email` and `idn-email` string formats, which generate Pydantic's `EmailStr` type.
238+
239+
!!! warning "Required Dependency"
240+
The `email` format requires the `email-validator` package to be installed:
241+
```bash
242+
pip install email-validator
243+
```
244+
Or install Pydantic with the email extra:
245+
```bash
246+
pip install pydantic[email]
247+
```
248+
249+
### Schema Example
250+
251+
```json
252+
{
253+
"type": "object",
254+
"properties": {
255+
"email": {
256+
"type": "string",
257+
"format": "email"
258+
}
259+
}
260+
}
261+
```
262+
263+
### Output
264+
265+
```python
266+
from pydantic import BaseModel, EmailStr
267+
268+
class MyModel(BaseModel):
269+
email: EmailStr
270+
```
271+
272+
### Avoiding the Dependency
273+
274+
If you don't want to install `email-validator`, you can map email formats to plain strings:
275+
276+
```bash
277+
datamodel-codegen --input schema.json --output models.py \
278+
--type-mappings "string+email=string" "string+idn-email=string"
279+
```
280+
281+
This generates `str` instead of `EmailStr`.
282+
283+
---
284+
285+
## ULID Format Support
286+
287+
The generator supports the `ulid` string format, which generates [`python-ulid`](https://github.com/mdomke/python-ulid) types.
288+
289+
!!! warning "Required Dependency"
290+
The `ulid` format requires the `python-ulid` package to be installed:
291+
```bash
292+
pip install python-ulid
293+
```
294+
For Pydantic integration, use:
295+
```bash
296+
pip install python-ulid[pydantic]
297+
```
298+
299+
### Schema Example
300+
301+
```json
302+
{
303+
"type": "object",
304+
"properties": {
305+
"id": {
306+
"type": "string",
307+
"format": "ulid"
308+
}
309+
}
310+
}
311+
```
312+
313+
### Output
314+
315+
```python
316+
from ulid import ULID
317+
from pydantic import BaseModel
318+
319+
class MyModel(BaseModel):
320+
id: ULID
321+
```
322+
323+
### What is ULID?
324+
325+
ULID (Universally Unique Lexicographically Sortable Identifier) is an alternative to UUID that offers:
326+
327+
- **Lexicographic sorting** - ULIDs sort naturally by creation time
328+
- **Compactness** - 26 characters vs 36 for UUID
329+
- **URL-safe** - Uses Crockford's Base32 encoding
330+
- **Timestamp encoded** - First 10 characters encode creation time
331+
332+
### When to use
333+
334+
- Distributed systems requiring time-ordered IDs
335+
- Applications where database index performance matters
336+
- When you need both uniqueness and sortability
337+
338+
---
339+
233340
## `--use-decimal-for-multiple-of`
234341

235342
Uses `Decimal` type for numbers with `multipleOf` constraints to avoid floating-point precision issues.
@@ -319,6 +426,7 @@ datamodel-codegen --input schema.json --output models.py \
319426
| `email` | `EmailStr` | `str` |
320427
| `idn-email` | `EmailStr` | `str` |
321428
| `uuid` | `UUID` | `str` |
429+
| `ulid` | `ULID` (requires `python-ulid`) | `str` |
322430
| `uri` | `AnyUrl` | `str` |
323431
| `binary` | `bytes` | `str` |
324432

0 commit comments

Comments
 (0)