sisyo 1.7.0 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +16 -16
- package/README.md +177 -166
- package/bin/known-hashes.json +8 -1
- package/bin/sisyo.js +285 -285
- package/package.json +30 -30
- package/templates/.claude/rules/agents-and-tests.md +32 -24
- package/templates/.claude/rules/docs.md +39 -19
- package/templates/.claude/skills/fastapi-best-practices/ASYNC-PATTERNS.md +121 -121
- package/templates/.claude/skills/fastapi-best-practices/DATABASE.md +153 -153
- package/templates/.claude/skills/fastapi-best-practices/DEPENDENCIES.md +140 -140
- package/templates/.claude/skills/fastapi-best-practices/PROJECT-STRUCTURE.md +90 -90
- package/templates/.claude/skills/fastapi-best-practices/PYDANTIC-GUIDE.md +155 -155
- package/templates/.claude/skills/fastapi-best-practices/SKILL.md +123 -123
- package/templates/.claude/skills/g6-style/SKILL.md +125 -125
- package/templates/.claude/skills/g6-style/references/checks.md +58 -58
- package/templates/.claude/skills/g6-style/references/imagery.md +103 -103
- package/templates/.claude/skills/g6-style/references/motion.md +121 -121
- package/templates/.claude/skills/g6-style/references/sections.md +263 -263
- package/templates/.claude/skills/g6-style/references/tokens.md +197 -197
- package/templates/.claude/skills/hindsight/SKILL.md +175 -175
- package/templates/.claude/skills/hoang-auth/SKILL.md +188 -0
- package/templates/.claude/skills/html-docs/SKILL.md +207 -207
- package/templates/.claude/skills/i18n-multilang/SKILL.md +153 -153
- package/templates/.claude/skills/i18n-multilang/assets/languages.json +47 -47
- package/templates/.claude/skills/i18n-multilang/assets/plural-vectors.json +46 -46
- package/templates/.claude/skills/i18n-multilang/assets/project/README.md +17 -17
- package/templates/.claude/skills/i18n-multilang/assets/project/gitattributes.txt +6 -6
- package/templates/.claude/skills/i18n-multilang/assets/project/gitignore.txt +4 -4
- package/templates/.claude/skills/i18n-multilang/assets/project/glossary.json +6 -6
- package/templates/.claude/skills/i18n-multilang/assets/project/i18n.config.json +22 -22
- package/templates/.claude/skills/i18n-multilang/assets/project/locales/__source__/common.json +10 -10
- package/templates/.claude/skills/i18n-multilang/assets/project/notes/common.json +5 -5
- package/templates/.claude/skills/i18n-multilang/assets/project/style.md +20 -20
- package/templates/.claude/skills/i18n-multilang/references/format.md +100 -100
- package/templates/.claude/skills/i18n-multilang/references/languages.md +97 -97
- package/templates/.claude/skills/i18n-multilang/references/layout-and-versioning.md +256 -256
- package/templates/.claude/skills/i18n-multilang/references/numbers.md +161 -161
- package/templates/.claude/skills/i18n-multilang/references/platform-unity.md +142 -142
- package/templates/.claude/skills/i18n-multilang/references/platform-web.md +107 -107
- package/templates/.claude/skills/i18n-multilang/references/translation.md +143 -143
- package/templates/.claude/skills/i18n-multilang/references/voice.md +271 -271
- package/templates/.claude/skills/i18n-multilang/scripts/i18n.py +1041 -1041
- package/templates/.claude/skills/i18n-multilang/scripts/numwords.py +1013 -1013
- package/templates/.claude/skills/i18n-multilang/scripts/voice.py +478 -478
- package/templates/.claude/skills/neon-game-ui-ux-skill/COMPONENTS.md +490 -490
- package/templates/.claude/skills/neon-game-ui-ux-skill/GAMIFICATION.md +246 -246
- package/templates/.claude/skills/neon-game-ui-ux-skill/ICONS.md +249 -249
- package/templates/.claude/skills/neon-game-ui-ux-skill/LAYOUT-NAV.md +362 -362
- package/templates/.claude/skills/neon-game-ui-ux-skill/MOTION-A11Y.md +168 -168
- package/templates/.claude/skills/neon-game-ui-ux-skill/SKILL.md +137 -137
- package/templates/.claude/skills/neon-game-ui-ux-skill/TOKENS.md +323 -323
- package/templates/.claude/skills/react-page-oriented/SKILL.md +407 -407
- package/templates/.claude/skills/react-page-oriented/triggers.md +12 -12
- package/templates/.claude/skills/terraform-best-practices/SKILL.md +341 -341
- package/templates/.claude/skills/typesafe-jev/SKILL.md +148 -148
- package/templates/.claude/skills/vibe-docs/SKILL.md +139 -242
- package/templates/SISYO.md +39 -37
- package/templates/docs/99_progress/features.md +7 -7
- package/templates/docs/99_progress/handoff.md +19 -19
- package/templates/docs/99_progress/todo.md +15 -15
- package/templates/docs/MAP.md +26 -26
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
# Project Structure
|
|
2
|
-
|
|
3
|
-
## Domain-Based Organization
|
|
4
|
-
|
|
5
|
-
```
|
|
6
|
-
fastapi-project/
|
|
7
|
-
├── alembic/ # migrations
|
|
8
|
-
├── src/
|
|
9
|
-
│ ├── auth/ # auth domain
|
|
10
|
-
│ │ ├── router.py # endpoints
|
|
11
|
-
│ │ ├── schemas.py # pydantic models
|
|
12
|
-
│ │ ├── models.py # db models
|
|
13
|
-
│ │ ├── service.py # business logic
|
|
14
|
-
│ │ ├── dependencies.py # route dependencies
|
|
15
|
-
│ │ ├── config.py # local config
|
|
16
|
-
│ │ ├── constants.py # error codes, enums
|
|
17
|
-
│ │ ├── exceptions.py # custom exceptions
|
|
18
|
-
│ │ └── utils.py # helpers
|
|
19
|
-
│ ├── posts/ # posts domain
|
|
20
|
-
│ │ └── ... (same structure)
|
|
21
|
-
│ ├── aws/ # external service
|
|
22
|
-
│ │ ├── client.py # API client
|
|
23
|
-
│ │ ├── schemas.py
|
|
24
|
-
│ │ └── ...
|
|
25
|
-
│ ├── config.py # global config
|
|
26
|
-
│ ├── models.py # shared models
|
|
27
|
-
│ ├── exceptions.py # global exceptions
|
|
28
|
-
│ ├── database.py # db connection
|
|
29
|
-
│ └── main.py # app initialization
|
|
30
|
-
├── tests/
|
|
31
|
-
│ ├── auth/
|
|
32
|
-
│ ├── posts/
|
|
33
|
-
│ └── conftest.py
|
|
34
|
-
├── requirements/
|
|
35
|
-
│ ├── base.txt
|
|
36
|
-
│ ├── dev.txt
|
|
37
|
-
│ └── prod.txt
|
|
38
|
-
├── .env
|
|
39
|
-
└── alembic.ini
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
## File Responsibilities
|
|
43
|
-
|
|
44
|
-
| File | Purpose |
|
|
45
|
-
|------|---------|
|
|
46
|
-
| `router.py` | All endpoints for the module |
|
|
47
|
-
| `schemas.py` | Pydantic request/response models |
|
|
48
|
-
| `models.py` | SQLAlchemy/database models |
|
|
49
|
-
| `service.py` | Business logic, DB operations |
|
|
50
|
-
| `dependencies.py` | FastAPI dependencies |
|
|
51
|
-
| `constants.py` | Enums, error codes |
|
|
52
|
-
| `config.py` | Environment-specific settings |
|
|
53
|
-
| `exceptions.py` | Custom HTTP exceptions |
|
|
54
|
-
| `utils.py` | Pure utility functions |
|
|
55
|
-
|
|
56
|
-
## Main App Setup
|
|
57
|
-
|
|
58
|
-
```python
|
|
59
|
-
# src/main.py
|
|
60
|
-
from fastapi import FastAPI
|
|
61
|
-
from src.auth.router import router as auth_router
|
|
62
|
-
from src.posts.router import router as posts_router
|
|
63
|
-
from src.config import settings
|
|
64
|
-
|
|
65
|
-
app_configs = {"title": "My API"}
|
|
66
|
-
|
|
67
|
-
# Hide docs in production
|
|
68
|
-
if settings.ENVIRONMENT not in ("local", "staging"):
|
|
69
|
-
app_configs["openapi_url"] = None
|
|
70
|
-
|
|
71
|
-
app = FastAPI(**app_configs)
|
|
72
|
-
|
|
73
|
-
app.include_router(auth_router, prefix="/auth", tags=["Auth"])
|
|
74
|
-
app.include_router(posts_router, prefix="/posts", tags=["Posts"])
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
## Cross-Module Imports
|
|
78
|
-
|
|
79
|
-
Always use explicit module names to avoid confusion:
|
|
80
|
-
|
|
81
|
-
```python
|
|
82
|
-
# GOOD
|
|
83
|
-
from src.auth import constants as auth_constants
|
|
84
|
-
from src.auth import service as auth_service
|
|
85
|
-
from src.notifications import service as notification_service
|
|
86
|
-
|
|
87
|
-
# BAD
|
|
88
|
-
from src.auth.constants import *
|
|
89
|
-
from ..auth import service # relative imports can be confusing
|
|
90
|
-
```
|
|
1
|
+
# Project Structure
|
|
2
|
+
|
|
3
|
+
## Domain-Based Organization
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
fastapi-project/
|
|
7
|
+
├── alembic/ # migrations
|
|
8
|
+
├── src/
|
|
9
|
+
│ ├── auth/ # auth domain
|
|
10
|
+
│ │ ├── router.py # endpoints
|
|
11
|
+
│ │ ├── schemas.py # pydantic models
|
|
12
|
+
│ │ ├── models.py # db models
|
|
13
|
+
│ │ ├── service.py # business logic
|
|
14
|
+
│ │ ├── dependencies.py # route dependencies
|
|
15
|
+
│ │ ├── config.py # local config
|
|
16
|
+
│ │ ├── constants.py # error codes, enums
|
|
17
|
+
│ │ ├── exceptions.py # custom exceptions
|
|
18
|
+
│ │ └── utils.py # helpers
|
|
19
|
+
│ ├── posts/ # posts domain
|
|
20
|
+
│ │ └── ... (same structure)
|
|
21
|
+
│ ├── aws/ # external service
|
|
22
|
+
│ │ ├── client.py # API client
|
|
23
|
+
│ │ ├── schemas.py
|
|
24
|
+
│ │ └── ...
|
|
25
|
+
│ ├── config.py # global config
|
|
26
|
+
│ ├── models.py # shared models
|
|
27
|
+
│ ├── exceptions.py # global exceptions
|
|
28
|
+
│ ├── database.py # db connection
|
|
29
|
+
│ └── main.py # app initialization
|
|
30
|
+
├── tests/
|
|
31
|
+
│ ├── auth/
|
|
32
|
+
│ ├── posts/
|
|
33
|
+
│ └── conftest.py
|
|
34
|
+
├── requirements/
|
|
35
|
+
│ ├── base.txt
|
|
36
|
+
│ ├── dev.txt
|
|
37
|
+
│ └── prod.txt
|
|
38
|
+
├── .env
|
|
39
|
+
└── alembic.ini
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## File Responsibilities
|
|
43
|
+
|
|
44
|
+
| File | Purpose |
|
|
45
|
+
|------|---------|
|
|
46
|
+
| `router.py` | All endpoints for the module |
|
|
47
|
+
| `schemas.py` | Pydantic request/response models |
|
|
48
|
+
| `models.py` | SQLAlchemy/database models |
|
|
49
|
+
| `service.py` | Business logic, DB operations |
|
|
50
|
+
| `dependencies.py` | FastAPI dependencies |
|
|
51
|
+
| `constants.py` | Enums, error codes |
|
|
52
|
+
| `config.py` | Environment-specific settings |
|
|
53
|
+
| `exceptions.py` | Custom HTTP exceptions |
|
|
54
|
+
| `utils.py` | Pure utility functions |
|
|
55
|
+
|
|
56
|
+
## Main App Setup
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
# src/main.py
|
|
60
|
+
from fastapi import FastAPI
|
|
61
|
+
from src.auth.router import router as auth_router
|
|
62
|
+
from src.posts.router import router as posts_router
|
|
63
|
+
from src.config import settings
|
|
64
|
+
|
|
65
|
+
app_configs = {"title": "My API"}
|
|
66
|
+
|
|
67
|
+
# Hide docs in production
|
|
68
|
+
if settings.ENVIRONMENT not in ("local", "staging"):
|
|
69
|
+
app_configs["openapi_url"] = None
|
|
70
|
+
|
|
71
|
+
app = FastAPI(**app_configs)
|
|
72
|
+
|
|
73
|
+
app.include_router(auth_router, prefix="/auth", tags=["Auth"])
|
|
74
|
+
app.include_router(posts_router, prefix="/posts", tags=["Posts"])
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Cross-Module Imports
|
|
78
|
+
|
|
79
|
+
Always use explicit module names to avoid confusion:
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
# GOOD
|
|
83
|
+
from src.auth import constants as auth_constants
|
|
84
|
+
from src.auth import service as auth_service
|
|
85
|
+
from src.notifications import service as notification_service
|
|
86
|
+
|
|
87
|
+
# BAD
|
|
88
|
+
from src.auth.constants import *
|
|
89
|
+
from ..auth import service # relative imports can be confusing
|
|
90
|
+
```
|
|
@@ -1,155 +1,155 @@
|
|
|
1
|
-
# Pydantic Guide
|
|
2
|
-
|
|
3
|
-
## Schema Design
|
|
4
|
-
|
|
5
|
-
### Request Validation
|
|
6
|
-
|
|
7
|
-
```python
|
|
8
|
-
from enum import StrEnum
|
|
9
|
-
from pydantic import BaseModel, Field, EmailStr, AnyUrl, field_validator
|
|
10
|
-
import re
|
|
11
|
-
|
|
12
|
-
class UserRole(StrEnum):
|
|
13
|
-
ADMIN = "admin"
|
|
14
|
-
USER = "user"
|
|
15
|
-
GUEST = "guest"
|
|
16
|
-
|
|
17
|
-
class UserCreate(BaseModel):
|
|
18
|
-
username: str = Field(min_length=1, max_length=128, pattern="^[A-Za-z0-9-_]+$")
|
|
19
|
-
email: EmailStr
|
|
20
|
-
age: int | None = Field(ge=18, default=None)
|
|
21
|
-
role: UserRole = UserRole.USER
|
|
22
|
-
website: AnyUrl | None = None
|
|
23
|
-
|
|
24
|
-
@field_validator("username")
|
|
25
|
-
@classmethod
|
|
26
|
-
def username_lowercase(cls, v: str) -> str:
|
|
27
|
-
return v.lower()
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
### Password Validation
|
|
31
|
-
|
|
32
|
-
```python
|
|
33
|
-
STRONG_PASSWORD_PATTERN = r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$"
|
|
34
|
-
|
|
35
|
-
class UserCreate(BaseModel):
|
|
36
|
-
password: str
|
|
37
|
-
|
|
38
|
-
@field_validator("password")
|
|
39
|
-
@classmethod
|
|
40
|
-
def validate_password(cls, v: str) -> str:
|
|
41
|
-
if not re.match(STRONG_PASSWORD_PATTERN, v):
|
|
42
|
-
raise ValueError(
|
|
43
|
-
"Password must contain at least one lowercase, "
|
|
44
|
-
"one uppercase, one digit, and one special character"
|
|
45
|
-
)
|
|
46
|
-
return v
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
## Custom Base Model
|
|
50
|
-
|
|
51
|
-
```python
|
|
52
|
-
from datetime import datetime
|
|
53
|
-
from zoneinfo import ZoneInfo
|
|
54
|
-
from pydantic import BaseModel, ConfigDict
|
|
55
|
-
from fastapi.encoders import jsonable_encoder
|
|
56
|
-
|
|
57
|
-
def datetime_to_gmt_str(dt: datetime) -> str:
|
|
58
|
-
if not dt.tzinfo:
|
|
59
|
-
dt = dt.replace(tzinfo=ZoneInfo("UTC"))
|
|
60
|
-
return dt.strftime("%Y-%m-%dT%H:%M:%S%z")
|
|
61
|
-
|
|
62
|
-
class CustomModel(BaseModel):
|
|
63
|
-
model_config = ConfigDict(
|
|
64
|
-
json_encoders={datetime: datetime_to_gmt_str},
|
|
65
|
-
populate_by_name=True,
|
|
66
|
-
from_attributes=True, # for ORM mode
|
|
67
|
-
)
|
|
68
|
-
|
|
69
|
-
def serializable_dict(self, **kwargs):
|
|
70
|
-
"""Return dict with only serializable fields."""
|
|
71
|
-
return jsonable_encoder(self.model_dump())
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
## Settings Management
|
|
75
|
-
|
|
76
|
-
Split settings by domain:
|
|
77
|
-
|
|
78
|
-
```python
|
|
79
|
-
# src/auth/config.py
|
|
80
|
-
from pydantic_settings import BaseSettings
|
|
81
|
-
|
|
82
|
-
class AuthConfig(BaseSettings):
|
|
83
|
-
JWT_ALG: str = "HS256"
|
|
84
|
-
JWT_SECRET: str
|
|
85
|
-
JWT_EXP: int = 5 # minutes
|
|
86
|
-
REFRESH_TOKEN_EXP: int = 60 * 24 * 30 # 30 days
|
|
87
|
-
SECURE_COOKIES: bool = True
|
|
88
|
-
|
|
89
|
-
auth_settings = AuthConfig()
|
|
90
|
-
|
|
91
|
-
# src/config.py
|
|
92
|
-
from pydantic import PostgresDsn, RedisDsn
|
|
93
|
-
from pydantic_settings import BaseSettings
|
|
94
|
-
|
|
95
|
-
class Config(BaseSettings):
|
|
96
|
-
DATABASE_URL: PostgresDsn
|
|
97
|
-
REDIS_URL: RedisDsn | None = None
|
|
98
|
-
ENVIRONMENT: str = "production"
|
|
99
|
-
CORS_ORIGINS: list[str] = []
|
|
100
|
-
|
|
101
|
-
settings = Config()
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
## Response Models
|
|
105
|
-
|
|
106
|
-
```python
|
|
107
|
-
# Separate input/output schemas
|
|
108
|
-
class UserCreate(BaseModel):
|
|
109
|
-
username: str
|
|
110
|
-
email: EmailStr
|
|
111
|
-
password: str
|
|
112
|
-
|
|
113
|
-
class UserResponse(BaseModel):
|
|
114
|
-
id: UUID4
|
|
115
|
-
username: str
|
|
116
|
-
email: EmailStr
|
|
117
|
-
created_at: datetime
|
|
118
|
-
|
|
119
|
-
model_config = ConfigDict(from_attributes=True)
|
|
120
|
-
|
|
121
|
-
class UserInDB(UserResponse):
|
|
122
|
-
hashed_password: str # internal only, never expose
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
## Nested Objects
|
|
126
|
-
|
|
127
|
-
```python
|
|
128
|
-
class Author(BaseModel):
|
|
129
|
-
id: UUID4
|
|
130
|
-
name: str
|
|
131
|
-
|
|
132
|
-
class PostResponse(BaseModel):
|
|
133
|
-
id: UUID4
|
|
134
|
-
title: str
|
|
135
|
-
author: Author # nested object
|
|
136
|
-
tags: list[str] = []
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
## Validation Error Messages
|
|
140
|
-
|
|
141
|
-
ValueError in Pydantic schemas becomes 422 Unprocessable Entity:
|
|
142
|
-
|
|
143
|
-
```python
|
|
144
|
-
class ProfileCreate(BaseModel):
|
|
145
|
-
username: str
|
|
146
|
-
|
|
147
|
-
@field_validator("username")
|
|
148
|
-
@classmethod
|
|
149
|
-
def validate_username(cls, v: str) -> str:
|
|
150
|
-
if v.lower() in ["admin", "root", "system"]:
|
|
151
|
-
raise ValueError("Username is reserved")
|
|
152
|
-
return v
|
|
153
|
-
|
|
154
|
-
# Response: {"detail": [{"loc": ["body", "username"], "msg": "Username is reserved", "type": "value_error"}]}
|
|
155
|
-
```
|
|
1
|
+
# Pydantic Guide
|
|
2
|
+
|
|
3
|
+
## Schema Design
|
|
4
|
+
|
|
5
|
+
### Request Validation
|
|
6
|
+
|
|
7
|
+
```python
|
|
8
|
+
from enum import StrEnum
|
|
9
|
+
from pydantic import BaseModel, Field, EmailStr, AnyUrl, field_validator
|
|
10
|
+
import re
|
|
11
|
+
|
|
12
|
+
class UserRole(StrEnum):
|
|
13
|
+
ADMIN = "admin"
|
|
14
|
+
USER = "user"
|
|
15
|
+
GUEST = "guest"
|
|
16
|
+
|
|
17
|
+
class UserCreate(BaseModel):
|
|
18
|
+
username: str = Field(min_length=1, max_length=128, pattern="^[A-Za-z0-9-_]+$")
|
|
19
|
+
email: EmailStr
|
|
20
|
+
age: int | None = Field(ge=18, default=None)
|
|
21
|
+
role: UserRole = UserRole.USER
|
|
22
|
+
website: AnyUrl | None = None
|
|
23
|
+
|
|
24
|
+
@field_validator("username")
|
|
25
|
+
@classmethod
|
|
26
|
+
def username_lowercase(cls, v: str) -> str:
|
|
27
|
+
return v.lower()
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Password Validation
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
STRONG_PASSWORD_PATTERN = r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$"
|
|
34
|
+
|
|
35
|
+
class UserCreate(BaseModel):
|
|
36
|
+
password: str
|
|
37
|
+
|
|
38
|
+
@field_validator("password")
|
|
39
|
+
@classmethod
|
|
40
|
+
def validate_password(cls, v: str) -> str:
|
|
41
|
+
if not re.match(STRONG_PASSWORD_PATTERN, v):
|
|
42
|
+
raise ValueError(
|
|
43
|
+
"Password must contain at least one lowercase, "
|
|
44
|
+
"one uppercase, one digit, and one special character"
|
|
45
|
+
)
|
|
46
|
+
return v
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Custom Base Model
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from datetime import datetime
|
|
53
|
+
from zoneinfo import ZoneInfo
|
|
54
|
+
from pydantic import BaseModel, ConfigDict
|
|
55
|
+
from fastapi.encoders import jsonable_encoder
|
|
56
|
+
|
|
57
|
+
def datetime_to_gmt_str(dt: datetime) -> str:
|
|
58
|
+
if not dt.tzinfo:
|
|
59
|
+
dt = dt.replace(tzinfo=ZoneInfo("UTC"))
|
|
60
|
+
return dt.strftime("%Y-%m-%dT%H:%M:%S%z")
|
|
61
|
+
|
|
62
|
+
class CustomModel(BaseModel):
|
|
63
|
+
model_config = ConfigDict(
|
|
64
|
+
json_encoders={datetime: datetime_to_gmt_str},
|
|
65
|
+
populate_by_name=True,
|
|
66
|
+
from_attributes=True, # for ORM mode
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
def serializable_dict(self, **kwargs):
|
|
70
|
+
"""Return dict with only serializable fields."""
|
|
71
|
+
return jsonable_encoder(self.model_dump())
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Settings Management
|
|
75
|
+
|
|
76
|
+
Split settings by domain:
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
# src/auth/config.py
|
|
80
|
+
from pydantic_settings import BaseSettings
|
|
81
|
+
|
|
82
|
+
class AuthConfig(BaseSettings):
|
|
83
|
+
JWT_ALG: str = "HS256"
|
|
84
|
+
JWT_SECRET: str
|
|
85
|
+
JWT_EXP: int = 5 # minutes
|
|
86
|
+
REFRESH_TOKEN_EXP: int = 60 * 24 * 30 # 30 days
|
|
87
|
+
SECURE_COOKIES: bool = True
|
|
88
|
+
|
|
89
|
+
auth_settings = AuthConfig()
|
|
90
|
+
|
|
91
|
+
# src/config.py
|
|
92
|
+
from pydantic import PostgresDsn, RedisDsn
|
|
93
|
+
from pydantic_settings import BaseSettings
|
|
94
|
+
|
|
95
|
+
class Config(BaseSettings):
|
|
96
|
+
DATABASE_URL: PostgresDsn
|
|
97
|
+
REDIS_URL: RedisDsn | None = None
|
|
98
|
+
ENVIRONMENT: str = "production"
|
|
99
|
+
CORS_ORIGINS: list[str] = []
|
|
100
|
+
|
|
101
|
+
settings = Config()
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Response Models
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
# Separate input/output schemas
|
|
108
|
+
class UserCreate(BaseModel):
|
|
109
|
+
username: str
|
|
110
|
+
email: EmailStr
|
|
111
|
+
password: str
|
|
112
|
+
|
|
113
|
+
class UserResponse(BaseModel):
|
|
114
|
+
id: UUID4
|
|
115
|
+
username: str
|
|
116
|
+
email: EmailStr
|
|
117
|
+
created_at: datetime
|
|
118
|
+
|
|
119
|
+
model_config = ConfigDict(from_attributes=True)
|
|
120
|
+
|
|
121
|
+
class UserInDB(UserResponse):
|
|
122
|
+
hashed_password: str # internal only, never expose
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Nested Objects
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
class Author(BaseModel):
|
|
129
|
+
id: UUID4
|
|
130
|
+
name: str
|
|
131
|
+
|
|
132
|
+
class PostResponse(BaseModel):
|
|
133
|
+
id: UUID4
|
|
134
|
+
title: str
|
|
135
|
+
author: Author # nested object
|
|
136
|
+
tags: list[str] = []
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Validation Error Messages
|
|
140
|
+
|
|
141
|
+
ValueError in Pydantic schemas becomes 422 Unprocessable Entity:
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
class ProfileCreate(BaseModel):
|
|
145
|
+
username: str
|
|
146
|
+
|
|
147
|
+
@field_validator("username")
|
|
148
|
+
@classmethod
|
|
149
|
+
def validate_username(cls, v: str) -> str:
|
|
150
|
+
if v.lower() in ["admin", "root", "system"]:
|
|
151
|
+
raise ValueError("Username is reserved")
|
|
152
|
+
return v
|
|
153
|
+
|
|
154
|
+
# Response: {"detail": [{"loc": ["body", "username"], "msg": "Username is reserved", "type": "value_error"}]}
|
|
155
|
+
```
|