@@ -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
235342Uses ` 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