pydantic-models-py
使用多模型模式构建 Pydantic 模型
无需手动编写样板代码即可定义一致的 API 架构。此技能可生成结构化的 Pydantic 模型,遵循已建立的请求验证、响应和数据库集成模式。
下载技能 ZIP
在 Claude 中上传
前往 设置 → 功能 → 技能 → 上传技能
开启并开始使用
测试它
正在使用“pydantic-models-py”。 User entity with id, name, email, created_at fields
预期结果:
生成具有正确 Field 定义、类型注解和 Config 类的 UserBase、UserCreate、UserUpdate、UserResponse 和 UserInDB 模型类,用于别名处理
正在使用“pydantic-models-py”。 Project model requiring camelCase API compatibility
预期结果:
具有 Field 别名(workspaceId、createdAt)的模型,可接受两种命名约定,同时在 Python 代码内部使用 Pythonic 的 snake_case
安全审计
安全All 21 static analysis findings are false positives. The scanner misidentified Markdown code block backticks as shell execution, and documentation references to HTTP/cryptography as actual code. SKILL.md is pure documentation with Python code examples for Pydantic model patterns. No executable code, network calls, or security risks detected.
质量评分
你能构建什么
REST API 架构定义
为 FastAPI 或 Flask 端点定义请求/响应模型,使用一致的验证模式。
数据库文档建模
创建带有 doc_type 字段的 InDB 模型变体,用于 Cosmos DB 或 MongoDB 文档存储。
前端-后端契约同步
生成匹配的 Python 模型和 TypeScript 类型以确保 API 契约一致性。
试试这些提示
Create Pydantic models for a User entity with fields: id (string), name (string, required), email (string with email validation), created_at (datetime). Use the multi-model pattern with Base, Create, Update, and Response variants.
Generate Pydantic models for a Project entity. All fields should accept both snake_case and camelCase using Field aliases. Include workspace_id, project_name, and is_active fields.
Create complete Pydantic models for a Document entity including InDB variant with doc_type field. Add min_length validation on name and optional description field for updates.
Build Pydantic models for an Order entity with nested line_items array. Each line item has product_id, quantity, and price. Create full multi-model pattern with validation constraints on all fields.
最佳实践
- 始终分离 Create 和 Update 模型 - Create 需要所有字段,Update 将所有字段设为可选以支持 PATCH 语义
- 在可选更新字段上使用 explicit default=None 的 Field,以区分 null 和未设置的值
- 在 Config 类中启用 populate_by_name = True,以接受来自 API 客户端的 snake_case 和 camelCase 两种命名方式
避免
- 不要将同一模型类重用于请求体和数据库文档 - 分离可防止数据泄露
- 避免在模型类中放置业务逻辑 - 将它们保持为仅包含验证的纯数据架构
- 在使用 Cosmos DB 时不要在 InDB 模型中省略 doc_type - 没有正确的类型过滤,查询将失败