|
| 1 | +# Pydantic v2 integration |
| 2 | + |
| 3 | +TypeID ships with an **optional Pydantic v2 adapter**. |
| 4 | +It lets you use `TypeID` in Pydantic models without pulling Pydantic into the TypeID core. |
| 5 | + |
| 6 | +The adapter: |
| 7 | + |
| 8 | +* validates values using the TypeID core, |
| 9 | +* optionally enforces a fixed prefix, |
| 10 | +* serializes TypeIDs as strings, |
| 11 | +* exposes sensible JSON Schema metadata. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +```bash |
| 18 | +pip install typeid-python[pydantic] |
| 19 | +``` |
| 20 | + |
| 21 | +This installs the latest version of Pydantic v2. |
| 22 | + |
| 23 | +## Basic usage |
| 24 | + |
| 25 | +Use `TypeIDField` with a fixed prefix. |
| 26 | + |
| 27 | +```python |
| 28 | +from typing import Literal |
| 29 | +from pydantic import BaseModel |
| 30 | +from typeid.integrations.pydantic import TypeIDField |
| 31 | + |
| 32 | +class User(BaseModel): |
| 33 | + id: TypeIDField[Literal["user"]] |
| 34 | + |
| 35 | +u = User(id="user_01ke82dtesfn9bjcrzyzz54ya9") |
| 36 | +assert str(u.id) == "user_01ke82dtesfn9bjcrzyzz54ya9" |
| 37 | +``` |
| 38 | + |
| 39 | +## Accepted inputs |
| 40 | + |
| 41 | +You can pass either a string or a `TypeID` instance. |
| 42 | + |
| 43 | +```python |
| 44 | +from typing import Literal |
| 45 | +from pydantic import BaseModel |
| 46 | +from typeid.integrations.pydantic import TypeIDField |
| 47 | + |
| 48 | +class User(BaseModel): |
| 49 | + id: TypeIDField[Literal["user"]] |
| 50 | + |
| 51 | +u = User(id="user_01ke82dtesfn9bjcrzyzz54ya9") |
| 52 | +assert u.id is not None |
| 53 | +``` |
| 54 | + |
| 55 | +```python |
| 56 | +from typing import Literal |
| 57 | +from pydantic import BaseModel |
| 58 | +from typeid import TypeID |
| 59 | +from typeid.integrations.pydantic import TypeIDField |
| 60 | + |
| 61 | +class User(BaseModel): |
| 62 | + id: TypeIDField[Literal["user"]] |
| 63 | + |
| 64 | +tid = TypeID.from_string("user_01ke82dtesfn9bjcrzyzz54ya9") |
| 65 | +u = User(id=tid) |
| 66 | + |
| 67 | +assert u.id == tid |
| 68 | +``` |
| 69 | + |
| 70 | +In both cases, `id` is stored as a `TypeID` object inside the model. |
| 71 | + |
| 72 | +## Prefix validation |
| 73 | + |
| 74 | +The prefix in `TypeIDField[...]` is enforced. |
| 75 | + |
| 76 | +```python |
| 77 | +import pytest |
| 78 | +from typing import Literal |
| 79 | +from pydantic import BaseModel, ValidationError |
| 80 | +from typeid.integrations.pydantic import TypeIDField |
| 81 | + |
| 82 | +class Order(BaseModel): |
| 83 | + id: TypeIDField[Literal["order"]] |
| 84 | + |
| 85 | +with pytest.raises(ValidationError): |
| 86 | + Order(id="user_01ke82dtesfn9bjcrzyzz54ya9") |
| 87 | +``` |
| 88 | + |
| 89 | +This fails with a validation error because the prefix does not match. |
| 90 | + |
| 91 | +This is useful when you want the model itself to encode domain meaning |
| 92 | +(e.g. *this field must be a user ID, not just any ID*). |
| 93 | + |
| 94 | +## Serialization |
| 95 | + |
| 96 | +When exporting a model, TypeIDs are always serialized as strings. |
| 97 | + |
| 98 | +```python |
| 99 | +from typing import Literal |
| 100 | +from pydantic import BaseModel |
| 101 | +from typeid.integrations.pydantic import TypeIDField |
| 102 | + |
| 103 | +class User(BaseModel): |
| 104 | + id: TypeIDField[Literal["user"]] |
| 105 | + |
| 106 | +u = User(id="user_01ke82dtesfn9bjcrzyzz54ya9") |
| 107 | +data = u.model_dump(mode="json") |
| 108 | + |
| 109 | +assert data == {"id": "user_01ke82dtesfn9bjcrzyzz54ya9"} |
| 110 | +``` |
| 111 | + |
| 112 | +```python |
| 113 | +from typing import Literal |
| 114 | +from pydantic import BaseModel |
| 115 | +from typeid.integrations.pydantic import TypeIDField |
| 116 | + |
| 117 | +class User(BaseModel): |
| 118 | + id: TypeIDField[Literal["user"]] |
| 119 | + |
| 120 | +u = User(id="user_01ke82dtesfn9bjcrzyzz54ya9") |
| 121 | +json_data = u.model_dump_json() |
| 122 | + |
| 123 | +assert json_data == '{"id":"user_01ke82dtesfn9bjcrzyzz54ya9"}' |
| 124 | +``` |
| 125 | + |
| 126 | +This keeps JSON output simple and predictable. |
| 127 | + |
| 128 | +## JSON Schema / OpenAPI |
| 129 | + |
| 130 | +The generated schema looks roughly like this: |
| 131 | + |
| 132 | +```yaml |
| 133 | +id: |
| 134 | + type: string |
| 135 | + format: typeid |
| 136 | + description: TypeID with prefix 'user' |
| 137 | + examples: |
| 138 | + - user_01ke82dtesfn9bjcrzyzz54ya9 |
| 139 | +``` |
| 140 | +
|
| 141 | +Notes: |
| 142 | +
|
| 143 | +* The schema does not hard-code internal regex details. |
| 144 | +* Actual validation is handled by the TypeID core. |
| 145 | +* The schema is meant to document intent, not re-implement parsing rules. |
| 146 | +
|
| 147 | +## Why `Literal["user"]`? |
| 148 | + |
| 149 | +The recommended form is: |
| 150 | + |
| 151 | +```text |
| 152 | +TypeIDField[Literal["user"]] |
| 153 | +``` |
| 154 | + |
| 155 | +This works cleanly with: |
| 156 | + |
| 157 | +* Ruff |
| 158 | +* Pyright / MyPy |
| 159 | +* IDE type checkers |
| 160 | + |
| 161 | +Using `Literal` makes the prefix a real compile-time constant and avoids |
| 162 | +annotation edge cases. |
| 163 | + |
| 164 | +## FastAPI |
| 165 | + |
| 166 | +FastAPI uses Pydantic v2, so no extra integration is needed. |
| 167 | + |
| 168 | +TypeID fields work automatically in request and response models, |
| 169 | +including OpenAPI output, as soon as you use them in a Pydantic model. |
| 170 | + |
| 171 | +## Design notes |
| 172 | + |
| 173 | +* The TypeID core does not import Pydantic. |
| 174 | +* All framework-specific code lives in `typeid.integrations.pydantic`. |
| 175 | +* Parsing and validation rules live in the core, not in the adapter. |
| 176 | + |
| 177 | +This keeps the integration small and easy to maintain. |
| 178 | + |
| 179 | +*That’s it — no magic, no hidden behavior.* |
0 commit comments