tribunal-kit 4.2.0 → 4.3.1
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/.agent/ARCHITECTURE.md +21 -14
- package/.agent/agents/swarm-worker-contracts.md +5 -5
- package/.agent/agents/ui-ux-auditor.md +292 -0
- package/.agent/rules/GEMINI.md +8 -8
- package/.agent/scripts/__pycache__/_colors.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/_utils.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/case_law_manager.cpython-311.pyc +0 -0
- package/.agent/scripts/_colors.js +18 -0
- package/.agent/scripts/_utils.js +42 -0
- package/.agent/scripts/auto_preview.js +197 -0
- package/.agent/scripts/bundle_analyzer.js +290 -0
- package/.agent/scripts/case_law_manager.js +684 -0
- package/.agent/scripts/checklist.js +266 -0
- package/.agent/scripts/colors.js +17 -0
- package/.agent/scripts/compress_skills.js +141 -0
- package/.agent/scripts/consolidate_skills.js +149 -0
- package/.agent/scripts/context_broker.js +609 -0
- package/.agent/scripts/deep_compress.js +150 -0
- package/.agent/scripts/dependency_analyzer.js +272 -0
- package/.agent/scripts/graph_builder.js +199 -0
- package/.agent/scripts/graph_zoom.js +154 -0
- package/.agent/scripts/inner_loop_validator.js +465 -0
- package/.agent/scripts/lint_runner.js +187 -0
- package/.agent/scripts/minify_context.js +100 -0
- package/.agent/scripts/patch_skills_meta.js +156 -0
- package/.agent/scripts/patch_skills_output.js +244 -0
- package/.agent/scripts/schema_validator.js +297 -0
- package/.agent/scripts/security_scan.js +303 -0
- package/.agent/scripts/session_manager.js +276 -0
- package/.agent/scripts/skill_evolution.js +644 -0
- package/.agent/scripts/skill_integrator.js +313 -0
- package/.agent/scripts/strengthen_skills.js +193 -0
- package/.agent/scripts/strip_tribunal.js +47 -0
- package/.agent/scripts/swarm_dispatcher.js +360 -0
- package/.agent/scripts/test_runner.js +193 -0
- package/.agent/scripts/utils.js +32 -0
- package/.agent/scripts/verify_all.js +256 -0
- package/.agent/skills/agent-organizer/SKILL.md +12 -4
- package/.agent/skills/agentic-patterns/SKILL.md +12 -4
- package/.agent/skills/ai-prompt-injection-defense/SKILL.md +12 -4
- package/.agent/skills/api-patterns/SKILL.md +209 -201
- package/.agent/skills/api-security-auditor/SKILL.md +12 -4
- package/.agent/skills/app-builder/SKILL.md +12 -4
- package/.agent/skills/app-builder/templates/SKILL.md +76 -68
- package/.agent/skills/app-builder/templates/astro-static/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/chrome-extension/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/cli-tool/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/electron-desktop/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/express-api/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/flutter-app/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/monorepo-turborepo/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/nextjs-fullstack/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/nextjs-saas/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/nextjs-static/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/nuxt-app/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/python-fastapi/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/react-native-app/TEMPLATE.md +1 -1
- package/.agent/skills/appflow-wireframe/SKILL.md +12 -4
- package/.agent/skills/architecture/SKILL.md +12 -4
- package/.agent/skills/authentication-best-practices/SKILL.md +12 -4
- package/.agent/skills/bash-linux/SKILL.md +12 -4
- package/.agent/skills/behavioral-modes/SKILL.md +12 -4
- package/.agent/skills/brainstorming/SKILL.md +12 -4
- package/.agent/skills/building-native-ui/SKILL.md +12 -4
- package/.agent/skills/clean-code/SKILL.md +12 -4
- package/.agent/skills/code-review-checklist/SKILL.md +12 -4
- package/.agent/skills/config-validator/SKILL.md +12 -4
- package/.agent/skills/csharp-developer/SKILL.md +12 -4
- package/.agent/skills/data-validation-schemas/SKILL.md +290 -282
- package/.agent/skills/database-design/SKILL.md +202 -194
- package/.agent/skills/deployment-procedures/SKILL.md +12 -4
- package/.agent/skills/devops-engineer/SKILL.md +12 -4
- package/.agent/skills/devops-incident-responder/SKILL.md +12 -4
- package/.agent/skills/doc.md +1 -1
- package/.agent/skills/documentation-templates/SKILL.md +12 -4
- package/.agent/skills/edge-computing/SKILL.md +12 -4
- package/.agent/skills/error-resilience/SKILL.md +390 -382
- package/.agent/skills/extract-design-system/SKILL.md +12 -4
- package/.agent/skills/framer-motion-expert/SKILL.md +206 -199
- package/.agent/skills/frontend-design/SKILL.md +163 -155
- package/.agent/skills/game-design-expert/SKILL.md +12 -4
- package/.agent/skills/game-engineering-expert/SKILL.md +12 -4
- package/.agent/skills/geo-fundamentals/SKILL.md +12 -4
- package/.agent/skills/github-operations/SKILL.md +12 -4
- package/.agent/skills/gsap-core/SKILL.md +54 -48
- package/.agent/skills/gsap-frameworks/SKILL.md +54 -48
- package/.agent/skills/gsap-performance/SKILL.md +54 -48
- package/.agent/skills/gsap-plugins/SKILL.md +54 -48
- package/.agent/skills/gsap-react/SKILL.md +54 -48
- package/.agent/skills/gsap-scrolltrigger/SKILL.md +54 -48
- package/.agent/skills/gsap-timeline/SKILL.md +54 -48
- package/.agent/skills/gsap-utils/SKILL.md +54 -48
- package/.agent/skills/i18n-localization/SKILL.md +12 -4
- package/.agent/skills/intelligent-routing/SKILL.md +41 -33
- package/.agent/skills/knowledge-graph/SKILL.md +36 -0
- package/.agent/skills/lint-and-validate/SKILL.md +12 -4
- package/.agent/skills/llm-engineering/SKILL.md +12 -4
- package/.agent/skills/local-first/SKILL.md +12 -4
- package/.agent/skills/mcp-builder/SKILL.md +12 -4
- package/.agent/skills/mobile-design/SKILL.md +225 -217
- package/.agent/skills/monorepo-management/SKILL.md +296 -288
- package/.agent/skills/motion-engineering/SKILL.md +195 -187
- package/.agent/skills/nextjs-react-expert/SKILL.md +196 -188
- package/.agent/skills/nodejs-best-practices/SKILL.md +12 -4
- package/.agent/skills/observability/SKILL.md +12 -4
- package/.agent/skills/parallel-agents/SKILL.md +12 -4
- package/.agent/skills/performance-profiling/SKILL.md +12 -4
- package/.agent/skills/plan-writing/SKILL.md +12 -4
- package/.agent/skills/platform-engineer/SKILL.md +12 -4
- package/.agent/skills/playwright-best-practices/SKILL.md +12 -4
- package/.agent/skills/powershell-windows/SKILL.md +12 -4
- package/.agent/skills/project-idioms/SKILL.md +12 -4
- package/.agent/skills/python-patterns/SKILL.md +12 -4
- package/.agent/skills/python-pro/SKILL.md +285 -277
- package/.agent/skills/react-specialist/SKILL.md +239 -231
- package/.agent/skills/readme-builder/SKILL.md +12 -4
- package/.agent/skills/realtime-patterns/SKILL.md +12 -4
- package/.agent/skills/red-team-tactics/SKILL.md +12 -4
- package/.agent/skills/rust-pro/SKILL.md +12 -4
- package/.agent/skills/seo-fundamentals/SKILL.md +12 -4
- package/.agent/skills/server-management/SKILL.md +12 -4
- package/.agent/skills/shadcn-ui-expert/SKILL.md +12 -4
- package/.agent/skills/skill-creator/SKILL.md +12 -4
- package/.agent/skills/sql-pro/SKILL.md +12 -4
- package/.agent/skills/supabase-postgres-best-practices/SKILL.md +12 -4
- package/.agent/skills/swiftui-expert/SKILL.md +12 -4
- package/.agent/skills/systematic-debugging/SKILL.md +12 -4
- package/.agent/skills/tailwind-patterns/SKILL.md +12 -4
- package/.agent/skills/tdd-workflow/SKILL.md +12 -4
- package/.agent/skills/test-result-analyzer/SKILL.md +12 -4
- package/.agent/skills/testing-patterns/SKILL.md +12 -4
- package/.agent/skills/trend-researcher/SKILL.md +12 -4
- package/.agent/skills/typescript-advanced/SKILL.md +297 -289
- package/.agent/skills/ui-ux-pro-max/SKILL.md +12 -4
- package/.agent/skills/ui-ux-researcher/SKILL.md +12 -4
- package/.agent/skills/vue-expert/SKILL.md +237 -229
- package/.agent/skills/vulnerability-scanner/SKILL.md +12 -4
- package/.agent/skills/web-accessibility-auditor/SKILL.md +12 -4
- package/.agent/skills/web-design-guidelines/SKILL.md +12 -4
- package/.agent/skills/webapp-testing/SKILL.md +12 -4
- package/.agent/skills/whimsy-injector/SKILL.md +12 -4
- package/.agent/skills/workflow-optimizer/SKILL.md +12 -4
- package/.agent/workflows/audit.md +6 -6
- package/.agent/workflows/deploy.md +1 -1
- package/.agent/workflows/generate.md +23 -6
- package/.agent/workflows/session.md +5 -5
- package/.agent/workflows/swarm.md +2 -2
- package/README.md +242 -186
- package/bin/tribunal-kit.js +297 -57
- package/package.json +81 -77
- package/scripts/changelog.js +167 -0
- package/scripts/sync-version.js +81 -0
- package/scripts/validate-payload.js +73 -0
- package/.agent/scripts/__pycache__/auto_preview.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/bundle_analyzer.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/checklist.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/dependency_analyzer.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/security_scan.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/session_manager.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/skill_integrator.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/swarm_dispatcher.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/test_runner.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/verify_all.cpython-311.pyc +0 -0
- package/.agent/scripts/auto_preview.py +0 -180
- package/.agent/scripts/bundle_analyzer.py +0 -259
- package/.agent/scripts/case_law_manager.py +0 -755
- package/.agent/scripts/checklist.py +0 -209
- package/.agent/scripts/compress_skills.py +0 -167
- package/.agent/scripts/consolidate_skills.py +0 -173
- package/.agent/scripts/deep_compress.py +0 -202
- package/.agent/scripts/dependency_analyzer.py +0 -247
- package/.agent/scripts/lint_runner.py +0 -188
- package/.agent/scripts/minify_context.py +0 -80
- package/.agent/scripts/patch_skills_meta.py +0 -177
- package/.agent/scripts/patch_skills_output.py +0 -285
- package/.agent/scripts/schema_validator.py +0 -279
- package/.agent/scripts/security_scan.py +0 -224
- package/.agent/scripts/session_manager.py +0 -261
- package/.agent/scripts/skill_evolution.py +0 -563
- package/.agent/scripts/skill_integrator.py +0 -234
- package/.agent/scripts/strengthen_skills.py +0 -220
- package/.agent/scripts/strip_tribunal.py +0 -41
- package/.agent/scripts/swarm_dispatcher.py +0 -350
- package/.agent/scripts/test_runner.py +0 -192
- package/.agent/scripts/test_swarm_dispatcher.py +0 -163
- package/.agent/scripts/verify_all.py +0 -195
|
@@ -1,286 +1,286 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: data-validation-schemas
|
|
3
|
-
description: Data validation and schema design mastery. Zod, Yup, Joi, Valibot, and Pydantic schema design, runtime type checking, API boundary validation, form validation patterns, DTO design, schema composition, error message formatting, schema evolution strategies, and coercion rules. Use when validating user input, API payloads, environment config, or any data crossing a trust boundary.
|
|
4
|
-
allowed-tools: Read, Write, Edit, Glob, Grep
|
|
5
|
-
version: 1.0.0
|
|
6
|
-
last-updated: 2026-04-17
|
|
7
|
-
applies-to-model: gemini-2.5-pro, claude-3-7-sonnet
|
|
8
|
-
---
|
|
9
|
-
|
|
10
|
-
## Hallucination Traps (Read First)
|
|
11
|
-
- ❌ Using `z.any()` or `z.unknown()` as a lazy escape hatch -> ✅ Always define the actual shape; `any` defeats the purpose of validation
|
|
12
|
-
- ❌ Validating on the client but not on the server -> ✅ Server validation is NOT optional — client validation is UX, server validation is security
|
|
13
|
-
- ❌ Throwing raw Zod errors to the client -> ✅ Format errors into user-friendly messages with `.flatten()` or `.format()`
|
|
14
|
-
|
|
15
|
-
---
|
|
16
|
-
|
|
17
|
-
# Data Validation & Schemas — Trust No Input
|
|
18
|
-
|
|
19
|
-
---
|
|
20
|
-
|
|
21
|
-
## The Golden Rule
|
|
22
|
-
|
|
23
|
-
```
|
|
24
|
-
Every trust boundary gets a schema.
|
|
25
|
-
No exceptions. No shortcuts. No "I'll add validation later."
|
|
26
|
-
|
|
27
|
-
Trust Boundaries:
|
|
28
|
-
✅ API request bodies (user → server)
|
|
29
|
-
✅ URL params / query strings (user → server)
|
|
30
|
-
✅ Environment variables (env → app)
|
|
31
|
-
✅ External API responses (3rd party → app)
|
|
32
|
-
✅ Database query results (DB → app, if untyped)
|
|
33
|
-
✅ File uploads (user → server)
|
|
34
|
-
✅ WebSocket messages (client → server)
|
|
35
|
-
✅ Form inputs (user → UI)
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
---
|
|
39
|
-
|
|
40
|
-
## Zod (Recommended — TypeScript)
|
|
41
|
-
|
|
42
|
-
### Basic Schemas
|
|
43
|
-
|
|
44
|
-
```typescript
|
|
45
|
-
import { z } from "zod";
|
|
46
|
-
|
|
47
|
-
// Primitives with constraints
|
|
48
|
-
const Email = z.string().email().toLowerCase().trim();
|
|
49
|
-
const Age = z.number().int().min(0).max(150);
|
|
50
|
-
const Username = z.string().min(3).max(30).regex(/^[a-zA-Z0-9_]+$/);
|
|
51
|
-
const URL = z.string().url().startsWith("https://");
|
|
52
|
-
|
|
53
|
-
// Object schema
|
|
54
|
-
const CreateUserSchema = z.object({
|
|
55
|
-
name: z.string().min(2).max(100),
|
|
56
|
-
email: Email,
|
|
57
|
-
age: Age.optional(),
|
|
58
|
-
role: z.enum(["admin", "editor", "viewer"]).default("viewer"),
|
|
59
|
-
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
// ✅ Infer TypeScript types from schemas (single source of truth)
|
|
63
|
-
type CreateUserInput = z.infer<typeof CreateUserSchema>;
|
|
64
|
-
// → { name: string; email: string; age?: number; role: "admin" | "editor" | "viewer"; ... }
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
### Composition & Reuse
|
|
68
|
-
|
|
69
|
-
```typescript
|
|
70
|
-
// ✅ Base schema + extend for variants
|
|
71
|
-
const BaseUserSchema = z.object({
|
|
72
|
-
name: z.string().min(2),
|
|
73
|
-
email: z.string().email(),
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
const CreateUserSchema = BaseUserSchema.extend({
|
|
77
|
-
password: z.string().min(8),
|
|
78
|
-
confirmPassword: z.string(),
|
|
79
|
-
}).refine((data) => data.password === data.confirmPassword, {
|
|
80
|
-
message: "Passwords don't match",
|
|
81
|
-
path: ["confirmPassword"],
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
const UpdateUserSchema = BaseUserSchema.partial(); // all fields optional
|
|
85
|
-
|
|
86
|
-
// ✅ Pick / Omit
|
|
87
|
-
const LoginSchema = BaseUserSchema.pick({ email: true }).extend({
|
|
88
|
-
password: z.string(),
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
// ✅ Merge two schemas
|
|
92
|
-
const FullProfileSchema = BaseUserSchema.merge(AddressSchema);
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
### API Boundary Validation
|
|
96
|
-
|
|
97
|
-
```typescript
|
|
98
|
-
// ✅ Server-side: validate at the boundary, type-safe downstream
|
|
99
|
-
import { z } from "zod";
|
|
100
|
-
|
|
101
|
-
// Define once, use everywhere
|
|
102
|
-
const QuerySchema = z.object({
|
|
103
|
-
page: z.coerce.number().int().min(1).default(1),
|
|
104
|
-
limit: z.coerce.number().int().min(1).max(100).default(20),
|
|
105
|
-
sort: z.enum(["created", "updated", "name"]).default("created"),
|
|
106
|
-
order: z.enum(["asc", "desc"]).default("desc"),
|
|
107
|
-
search: z.string().max(200).optional(),
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
// Express middleware
|
|
111
|
-
function validate<T extends z.ZodType>(schema: T) {
|
|
112
|
-
return (req: Request, res: Response, next: NextFunction) => {
|
|
113
|
-
const result = schema.safeParse(req.body);
|
|
114
|
-
if (!result.success) {
|
|
115
|
-
return res.status(400).json({
|
|
116
|
-
error: "Validation failed",
|
|
117
|
-
issues: result.error.flatten().fieldErrors,
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
req.body = result.data; // ✅ Validated + coerced data replaces raw body
|
|
121
|
-
next();
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
app.post("/api/users", validate(CreateUserSchema), async (req, res) => {
|
|
126
|
-
// req.body is now fully typed and validated
|
|
127
|
-
const user = await createUser(req.body);
|
|
128
|
-
res.status(201).json(user);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
// ❌ BAD: Validating inside the handler
|
|
132
|
-
// ✅ GOOD: Validation as middleware — keeps handlers clean
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
### Error Formatting
|
|
136
|
-
|
|
137
|
-
```typescript
|
|
138
|
-
// ✅ User-friendly error messages
|
|
139
|
-
const result = CreateUserSchema.safeParse(rawInput);
|
|
140
|
-
|
|
141
|
-
if (!result.success) {
|
|
142
|
-
// .flatten() — flat structure for simple forms
|
|
143
|
-
const flat = result.error.flatten();
|
|
144
|
-
// { fieldErrors: { email: ["Invalid email"], name: ["Too short"] } }
|
|
145
|
-
|
|
146
|
-
// .format() — nested structure matching schema shape
|
|
147
|
-
const formatted = result.error.format();
|
|
148
|
-
// { email: { _errors: ["Invalid email"] }, name: { _errors: ["Too short"] } }
|
|
149
|
-
|
|
150
|
-
// Custom error map (global)
|
|
151
|
-
z.setErrorMap((issue, ctx) => {
|
|
152
|
-
if (issue.code === z.ZodIssueCode.too_small) {
|
|
153
|
-
return { message: `Must be at least ${issue.minimum} characters` };
|
|
154
|
-
}
|
|
155
|
-
return { message: ctx.defaultError };
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
---
|
|
161
|
-
|
|
162
|
-
## Environment Validation (Fail Fast)
|
|
163
|
-
|
|
164
|
-
```typescript
|
|
165
|
-
// ✅ Validate ALL env vars at startup — crash immediately if invalid
|
|
166
|
-
const EnvSchema = z.object({
|
|
167
|
-
NODE_ENV: z.enum(["development", "production", "test"]),
|
|
168
|
-
PORT: z.coerce.number().default(3000),
|
|
169
|
-
DATABASE_URL: z.string().url(),
|
|
170
|
-
REDIS_URL: z.string().url().optional(),
|
|
171
|
-
JWT_SECRET: z.string().min(32, "JWT_SECRET must be ≥ 32 characters"),
|
|
172
|
-
API_KEY: z.string().min(1),
|
|
173
|
-
LOG_LEVEL: z.enum(["debug", "info", "warn", "error"]).default("info"),
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
export const env = EnvSchema.parse(process.env);
|
|
177
|
-
|
|
178
|
-
// ❌ TRAP: process.env.DATABASE_URL! ← crashes at RUNTIME, not startup
|
|
179
|
-
// ✅ Parse at module load → crash at STARTUP with clear error message
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
---
|
|
183
|
-
|
|
184
|
-
## Pydantic (Python)
|
|
185
|
-
|
|
186
|
-
```python
|
|
187
|
-
from pydantic import BaseModel, Field, field_validator, model_validator
|
|
188
|
-
from datetime import datetime
|
|
189
|
-
|
|
190
|
-
class CreateUserRequest(BaseModel):
|
|
191
|
-
name: str = Field(min_length=2, max_length=100)
|
|
192
|
-
email: str = Field(pattern=r"^[\w\.\+\-]+@[\w]+\.[\w\.]+$")
|
|
193
|
-
age: int | None = Field(default=None, ge=0, le=150)
|
|
194
|
-
role: str = Field(default="viewer")
|
|
195
|
-
|
|
196
|
-
@field_validator("email")
|
|
197
|
-
@classmethod
|
|
198
|
-
def normalize_email(cls, v: str) -> str:
|
|
199
|
-
return v.lower().strip()
|
|
200
|
-
|
|
201
|
-
@field_validator("role")
|
|
202
|
-
@classmethod
|
|
203
|
-
def validate_role(cls, v: str) -> str:
|
|
204
|
-
allowed = {"admin", "editor", "viewer"}
|
|
205
|
-
if v not in allowed:
|
|
206
|
-
raise ValueError(f"Role must be one of: {allowed}")
|
|
207
|
-
return v
|
|
208
|
-
|
|
209
|
-
# FastAPI uses Pydantic automatically
|
|
210
|
-
@app.post("/users")
|
|
211
|
-
async def create_user(user: CreateUserRequest):
|
|
212
|
-
# user is already validated and typed
|
|
213
|
-
return await db.create_user(user.model_dump())
|
|
214
|
-
```
|
|
215
|
-
|
|
216
|
-
---
|
|
217
|
-
|
|
218
|
-
## Form Validation (React + Zod)
|
|
219
|
-
|
|
220
|
-
```tsx
|
|
221
|
-
// ✅ React Hook Form + Zod = type-safe forms
|
|
222
|
-
import { useForm } from "react-hook-form";
|
|
223
|
-
import { zodResolver } from "@hookform/resolvers/zod";
|
|
224
|
-
import { z } from "zod";
|
|
225
|
-
|
|
226
|
-
const SignupSchema = z.object({
|
|
227
|
-
email: z.string().email("Invalid email address"),
|
|
228
|
-
password: z.string()
|
|
229
|
-
.min(8, "Password must be at least 8 characters")
|
|
230
|
-
.regex(/[A-Z]/, "Must contain uppercase letter")
|
|
231
|
-
.regex(/[0-9]/, "Must contain a number"),
|
|
232
|
-
terms: z.literal(true, {
|
|
233
|
-
errorMap: () => ({ message: "You must accept the terms" }),
|
|
234
|
-
}),
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
type SignupData = z.infer<typeof SignupSchema>;
|
|
238
|
-
|
|
239
|
-
function SignupForm() {
|
|
240
|
-
const { register, handleSubmit, formState: { errors } } = useForm<SignupData>({
|
|
241
|
-
resolver: zodResolver(SignupSchema),
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
return (
|
|
245
|
-
<form onSubmit={handleSubmit((data) => signup(data))}>
|
|
246
|
-
<input {...register("email")} />
|
|
247
|
-
{errors.email && <span>{errors.email.message}</span>}
|
|
248
|
-
|
|
249
|
-
<input type="password" {...register("password")} />
|
|
250
|
-
{errors.password && <span>{errors.password.message}</span>}
|
|
251
|
-
|
|
252
|
-
<label>
|
|
253
|
-
<input type="checkbox" {...register("terms")} />
|
|
254
|
-
I accept the terms
|
|
255
|
-
</label>
|
|
256
|
-
{errors.terms && <span>{errors.terms.message}</span>}
|
|
257
|
-
|
|
258
|
-
<button type="submit">Sign Up</button>
|
|
259
|
-
</form>
|
|
260
|
-
);
|
|
261
|
-
}
|
|
262
|
-
```
|
|
263
|
-
|
|
264
|
-
---
|
|
265
|
-
|
|
266
|
-
## Schema Anti-Patterns
|
|
267
|
-
|
|
268
|
-
```
|
|
269
|
-
❌ z.any() / z.unknown() as a lazy escape — defeats the purpose
|
|
270
|
-
❌ Validating on client only — server is the security boundary
|
|
271
|
-
❌ Different schemas for same entity on client vs server — drift guaranteed
|
|
272
|
-
❌ Coercing without documenting — z.coerce.number() silently converts "abc" → NaN
|
|
273
|
-
❌ Skipping .safeParse() in user-facing code — .parse() throws, bad UX
|
|
274
|
-
❌ Giant monolithic schemas — use .extend(), .pick(), .merge() for composition
|
|
275
|
-
❌ Not validating 3rd-party API responses — "they'll always return what docs say"
|
|
276
|
-
```
|
|
277
|
-
|
|
278
|
-
---
|
|
1
|
+
---
|
|
2
|
+
name: data-validation-schemas
|
|
3
|
+
description: Data validation and schema design mastery. Zod, Yup, Joi, Valibot, and Pydantic schema design, runtime type checking, API boundary validation, form validation patterns, DTO design, schema composition, error message formatting, schema evolution strategies, and coercion rules. Use when validating user input, API payloads, environment config, or any data crossing a trust boundary.
|
|
4
|
+
allowed-tools: Read, Write, Edit, Glob, Grep
|
|
5
|
+
version: 1.0.0
|
|
6
|
+
last-updated: 2026-04-17
|
|
7
|
+
applies-to-model: gemini-2.5-pro, claude-3-7-sonnet
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Hallucination Traps (Read First)
|
|
11
|
+
- ❌ Using `z.any()` or `z.unknown()` as a lazy escape hatch -> ✅ Always define the actual shape; `any` defeats the purpose of validation
|
|
12
|
+
- ❌ Validating on the client but not on the server -> ✅ Server validation is NOT optional — client validation is UX, server validation is security
|
|
13
|
+
- ❌ Throwing raw Zod errors to the client -> ✅ Format errors into user-friendly messages with `.flatten()` or `.format()`
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
# Data Validation & Schemas — Trust No Input
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## The Golden Rule
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
Every trust boundary gets a schema.
|
|
25
|
+
No exceptions. No shortcuts. No "I'll add validation later."
|
|
26
|
+
|
|
27
|
+
Trust Boundaries:
|
|
28
|
+
✅ API request bodies (user → server)
|
|
29
|
+
✅ URL params / query strings (user → server)
|
|
30
|
+
✅ Environment variables (env → app)
|
|
31
|
+
✅ External API responses (3rd party → app)
|
|
32
|
+
✅ Database query results (DB → app, if untyped)
|
|
33
|
+
✅ File uploads (user → server)
|
|
34
|
+
✅ WebSocket messages (client → server)
|
|
35
|
+
✅ Form inputs (user → UI)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## Zod (Recommended — TypeScript)
|
|
41
|
+
|
|
42
|
+
### Basic Schemas
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { z } from "zod";
|
|
46
|
+
|
|
47
|
+
// Primitives with constraints
|
|
48
|
+
const Email = z.string().email().toLowerCase().trim();
|
|
49
|
+
const Age = z.number().int().min(0).max(150);
|
|
50
|
+
const Username = z.string().min(3).max(30).regex(/^[a-zA-Z0-9_]+$/);
|
|
51
|
+
const URL = z.string().url().startsWith("https://");
|
|
52
|
+
|
|
53
|
+
// Object schema
|
|
54
|
+
const CreateUserSchema = z.object({
|
|
55
|
+
name: z.string().min(2).max(100),
|
|
56
|
+
email: Email,
|
|
57
|
+
age: Age.optional(),
|
|
58
|
+
role: z.enum(["admin", "editor", "viewer"]).default("viewer"),
|
|
59
|
+
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// ✅ Infer TypeScript types from schemas (single source of truth)
|
|
63
|
+
type CreateUserInput = z.infer<typeof CreateUserSchema>;
|
|
64
|
+
// → { name: string; email: string; age?: number; role: "admin" | "editor" | "viewer"; ... }
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Composition & Reuse
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
// ✅ Base schema + extend for variants
|
|
71
|
+
const BaseUserSchema = z.object({
|
|
72
|
+
name: z.string().min(2),
|
|
73
|
+
email: z.string().email(),
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const CreateUserSchema = BaseUserSchema.extend({
|
|
77
|
+
password: z.string().min(8),
|
|
78
|
+
confirmPassword: z.string(),
|
|
79
|
+
}).refine((data) => data.password === data.confirmPassword, {
|
|
80
|
+
message: "Passwords don't match",
|
|
81
|
+
path: ["confirmPassword"],
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const UpdateUserSchema = BaseUserSchema.partial(); // all fields optional
|
|
85
|
+
|
|
86
|
+
// ✅ Pick / Omit
|
|
87
|
+
const LoginSchema = BaseUserSchema.pick({ email: true }).extend({
|
|
88
|
+
password: z.string(),
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// ✅ Merge two schemas
|
|
92
|
+
const FullProfileSchema = BaseUserSchema.merge(AddressSchema);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### API Boundary Validation
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
// ✅ Server-side: validate at the boundary, type-safe downstream
|
|
99
|
+
import { z } from "zod";
|
|
100
|
+
|
|
101
|
+
// Define once, use everywhere
|
|
102
|
+
const QuerySchema = z.object({
|
|
103
|
+
page: z.coerce.number().int().min(1).default(1),
|
|
104
|
+
limit: z.coerce.number().int().min(1).max(100).default(20),
|
|
105
|
+
sort: z.enum(["created", "updated", "name"]).default("created"),
|
|
106
|
+
order: z.enum(["asc", "desc"]).default("desc"),
|
|
107
|
+
search: z.string().max(200).optional(),
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Express middleware
|
|
111
|
+
function validate<T extends z.ZodType>(schema: T) {
|
|
112
|
+
return (req: Request, res: Response, next: NextFunction) => {
|
|
113
|
+
const result = schema.safeParse(req.body);
|
|
114
|
+
if (!result.success) {
|
|
115
|
+
return res.status(400).json({
|
|
116
|
+
error: "Validation failed",
|
|
117
|
+
issues: result.error.flatten().fieldErrors,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
req.body = result.data; // ✅ Validated + coerced data replaces raw body
|
|
121
|
+
next();
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
app.post("/api/users", validate(CreateUserSchema), async (req, res) => {
|
|
126
|
+
// req.body is now fully typed and validated
|
|
127
|
+
const user = await createUser(req.body);
|
|
128
|
+
res.status(201).json(user);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// ❌ BAD: Validating inside the handler
|
|
132
|
+
// ✅ GOOD: Validation as middleware — keeps handlers clean
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Error Formatting
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
// ✅ User-friendly error messages
|
|
139
|
+
const result = CreateUserSchema.safeParse(rawInput);
|
|
140
|
+
|
|
141
|
+
if (!result.success) {
|
|
142
|
+
// .flatten() — flat structure for simple forms
|
|
143
|
+
const flat = result.error.flatten();
|
|
144
|
+
// { fieldErrors: { email: ["Invalid email"], name: ["Too short"] } }
|
|
145
|
+
|
|
146
|
+
// .format() — nested structure matching schema shape
|
|
147
|
+
const formatted = result.error.format();
|
|
148
|
+
// { email: { _errors: ["Invalid email"] }, name: { _errors: ["Too short"] } }
|
|
149
|
+
|
|
150
|
+
// Custom error map (global)
|
|
151
|
+
z.setErrorMap((issue, ctx) => {
|
|
152
|
+
if (issue.code === z.ZodIssueCode.too_small) {
|
|
153
|
+
return { message: `Must be at least ${issue.minimum} characters` };
|
|
154
|
+
}
|
|
155
|
+
return { message: ctx.defaultError };
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
```
|
|
279
159
|
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
## Environment Validation (Fail Fast)
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
// ✅ Validate ALL env vars at startup — crash immediately if invalid
|
|
166
|
+
const EnvSchema = z.object({
|
|
167
|
+
NODE_ENV: z.enum(["development", "production", "test"]),
|
|
168
|
+
PORT: z.coerce.number().default(3000),
|
|
169
|
+
DATABASE_URL: z.string().url(),
|
|
170
|
+
REDIS_URL: z.string().url().optional(),
|
|
171
|
+
JWT_SECRET: z.string().min(32, "JWT_SECRET must be ≥ 32 characters"),
|
|
172
|
+
API_KEY: z.string().min(1),
|
|
173
|
+
LOG_LEVEL: z.enum(["debug", "info", "warn", "error"]).default("info"),
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
export const env = EnvSchema.parse(process.env);
|
|
177
|
+
|
|
178
|
+
// ❌ TRAP: process.env.DATABASE_URL! ← crashes at RUNTIME, not startup
|
|
179
|
+
// ✅ Parse at module load → crash at STARTUP with clear error message
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
---
|
|
183
|
+
|
|
184
|
+
## Pydantic (Python)
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
from pydantic import BaseModel, Field, field_validator, model_validator
|
|
188
|
+
from datetime import datetime
|
|
189
|
+
|
|
190
|
+
class CreateUserRequest(BaseModel):
|
|
191
|
+
name: str = Field(min_length=2, max_length=100)
|
|
192
|
+
email: str = Field(pattern=r"^[\w\.\+\-]+@[\w]+\.[\w\.]+$")
|
|
193
|
+
age: int | None = Field(default=None, ge=0, le=150)
|
|
194
|
+
role: str = Field(default="viewer")
|
|
195
|
+
|
|
196
|
+
@field_validator("email")
|
|
197
|
+
@classmethod
|
|
198
|
+
def normalize_email(cls, v: str) -> str:
|
|
199
|
+
return v.lower().strip()
|
|
200
|
+
|
|
201
|
+
@field_validator("role")
|
|
202
|
+
@classmethod
|
|
203
|
+
def validate_role(cls, v: str) -> str:
|
|
204
|
+
allowed = {"admin", "editor", "viewer"}
|
|
205
|
+
if v not in allowed:
|
|
206
|
+
raise ValueError(f"Role must be one of: {allowed}")
|
|
207
|
+
return v
|
|
208
|
+
|
|
209
|
+
# FastAPI uses Pydantic automatically
|
|
210
|
+
@app.post("/users")
|
|
211
|
+
async def create_user(user: CreateUserRequest):
|
|
212
|
+
# user is already validated and typed
|
|
213
|
+
return await db.create_user(user.model_dump())
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
## Form Validation (React + Zod)
|
|
219
|
+
|
|
220
|
+
```tsx
|
|
221
|
+
// ✅ React Hook Form + Zod = type-safe forms
|
|
222
|
+
import { useForm } from "react-hook-form";
|
|
223
|
+
import { zodResolver } from "@hookform/resolvers/zod";
|
|
224
|
+
import { z } from "zod";
|
|
225
|
+
|
|
226
|
+
const SignupSchema = z.object({
|
|
227
|
+
email: z.string().email("Invalid email address"),
|
|
228
|
+
password: z.string()
|
|
229
|
+
.min(8, "Password must be at least 8 characters")
|
|
230
|
+
.regex(/[A-Z]/, "Must contain uppercase letter")
|
|
231
|
+
.regex(/[0-9]/, "Must contain a number"),
|
|
232
|
+
terms: z.literal(true, {
|
|
233
|
+
errorMap: () => ({ message: "You must accept the terms" }),
|
|
234
|
+
}),
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
type SignupData = z.infer<typeof SignupSchema>;
|
|
238
|
+
|
|
239
|
+
function SignupForm() {
|
|
240
|
+
const { register, handleSubmit, formState: { errors } } = useForm<SignupData>({
|
|
241
|
+
resolver: zodResolver(SignupSchema),
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
return (
|
|
245
|
+
<form onSubmit={handleSubmit((data) => signup(data))}>
|
|
246
|
+
<input {...register("email")} />
|
|
247
|
+
{errors.email && <span>{errors.email.message}</span>}
|
|
248
|
+
|
|
249
|
+
<input type="password" {...register("password")} />
|
|
250
|
+
{errors.password && <span>{errors.password.message}</span>}
|
|
251
|
+
|
|
252
|
+
<label>
|
|
253
|
+
<input type="checkbox" {...register("terms")} />
|
|
254
|
+
I accept the terms
|
|
255
|
+
</label>
|
|
256
|
+
{errors.terms && <span>{errors.terms.message}</span>}
|
|
257
|
+
|
|
258
|
+
<button type="submit">Sign Up</button>
|
|
259
|
+
</form>
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
```
|
|
280
263
|
|
|
281
264
|
---
|
|
282
265
|
|
|
283
|
-
##
|
|
266
|
+
## Schema Anti-Patterns
|
|
267
|
+
|
|
268
|
+
```
|
|
269
|
+
❌ z.any() / z.unknown() as a lazy escape — defeats the purpose
|
|
270
|
+
❌ Validating on client only — server is the security boundary
|
|
271
|
+
❌ Different schemas for same entity on client vs server — drift guaranteed
|
|
272
|
+
❌ Coercing without documenting — z.coerce.number() silently converts "abc" → NaN
|
|
273
|
+
❌ Skipping .safeParse() in user-facing code — .parse() throws, bad UX
|
|
274
|
+
❌ Giant monolithic schemas — use .extend(), .pick(), .merge() for composition
|
|
275
|
+
❌ Not validating 3rd-party API responses — "they'll always return what docs say"
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
---
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
|
|
284
284
|
|
|
285
285
|
AI coding assistants often fall into specific bad habits when dealing with this domain. These are strictly forbidden:
|
|
286
286
|
|
|
@@ -292,7 +292,7 @@ AI coding assistants often fall into specific bad habits when dealing with this
|
|
|
292
292
|
|
|
293
293
|
---
|
|
294
294
|
|
|
295
|
-
|
|
295
|
+
|
|
296
296
|
|
|
297
297
|
**Slash command: `/review` or `/tribunal-full`**
|
|
298
298
|
**Active reviewers: `logic-reviewer` · `security-auditor`**
|
|
@@ -303,7 +303,7 @@ AI coding assistants often fall into specific bad habits when dealing with this
|
|
|
303
303
|
2. **Silent Degradation:** Catching and suppressing errors without logging or handling.
|
|
304
304
|
3. **Context Amnesia:** Forgetting the user's constraints and offering generic advice instead of tailored solutions.
|
|
305
305
|
|
|
306
|
-
|
|
306
|
+
|
|
307
307
|
|
|
308
308
|
Review these questions before confirming output:
|
|
309
309
|
```
|
|
@@ -317,4 +317,12 @@ Review these questions before confirming output:
|
|
|
317
317
|
|
|
318
318
|
**CRITICAL:** You must follow a strict "evidence-based closeout" state machine.
|
|
319
319
|
- ❌ **Forbidden:** Declaring a task complete because the output "looks correct."
|
|
320
|
-
- ✅ **Required:** You are explicitly forbidden from finalizing any task without providing **concrete evidence** (terminal output, passing tests, compile success, or equivalent proof) that your output works as intended.
|
|
320
|
+
- ✅ **Required:** You are explicitly forbidden from finalizing any task without providing **concrete evidence** (terminal output, passing tests, compile success, or equivalent proof) that your output works as intended.
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
## Pre-Flight Checklist
|
|
324
|
+
- [ ] Have I reviewed the user's specific constraints and requests?
|
|
325
|
+
- [ ] Have I checked the environment for relevant existing implementations?
|
|
326
|
+
|
|
327
|
+
## VBC Protocol (Verification-Before-Completion)
|
|
328
|
+
You MUST verify existing code signatures and variables before attempting to modify or call them. No hallucination is permitted.
|