feat: v2.0 iteration — test coverage, reliability, observability
M0: Test coverage expansion (151 → 321 tests, 61% → 66% lines) - normalizeKnowledge CRLF/LF normalization fix - gooseReviewer: 11 tests, 97.35% coverage - r2 storage: 22 tests, 100% coverage - triage processor: 11 tests, exported buildTriagePrompt/labelName - webhook processor: 35 tests, 72.39% coverage - conflict resolver: 12 tests - review processor: 7 tests M1: Reliability hardening - Smart retry with jitter, maxTotalTimeoutMs, custom retryable predicate - MergeGuard: in-memory merge dedup tracker (100% coverage) - failureMessages: 7-category failure classifier (100% coverage) M2: Observability - MetricsCollector: Actions-run metrics with snapshot/markdown/reset - 11 tests, 100% coverage New files: 12 test files, 3 source modules All gates: typecheck ✓ lint ✓ format ✓ 321/321 tests ✓ build ✓
This commit is contained in:
1 parent
db53296d78
commit
78bf8e9e41
87 files changed
+6003
-2587
No files matched your search
@@ -13,50 +13,50 @@ tags: parse, json, security, type-safety
|
||||
|
||||
```typescript
|
||||
// JSON.parse returns any - no type safety
|
||||
const config = JSON.parse(fs.readFileSync('config.json', 'utf-8'))
|
||||
const config = JSON.parse(fs.readFileSync("config.json", "utf-8"));
|
||||
// config is 'any' - TypeScript allows anything
|
||||
|
||||
// This might crash at runtime if structure changed
|
||||
console.log(config.database.host) // TypeError: Cannot read property 'host' of undefined
|
||||
console.log(config.database.host); // TypeError: Cannot read property 'host' of undefined
|
||||
|
||||
// API response - also unvalidated
|
||||
const response = await fetch('/api/user')
|
||||
const user = await response.json() // any type
|
||||
console.log(user.name.toUpperCase()) // Crash if name is null/undefined
|
||||
const response = await fetch("/api/user");
|
||||
const user = await response.json(); // any type
|
||||
console.log(user.name.toUpperCase()); // Crash if name is null/undefined
|
||||
```
|
||||
|
||||
**Correct (validate after JSON.parse):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
const configSchema = z.object({
|
||||
database: z.object({
|
||||
host: z.string(),
|
||||
port: z.number(),
|
||||
name: z.string(),
|
||||
name: z.string()
|
||||
}),
|
||||
api: z.object({
|
||||
key: z.string(),
|
||||
timeout: z.number().default(5000),
|
||||
}),
|
||||
})
|
||||
timeout: z.number().default(5000)
|
||||
})
|
||||
});
|
||||
|
||||
// Parse JSON then validate
|
||||
const rawConfig = JSON.parse(fs.readFileSync('config.json', 'utf-8'))
|
||||
const config = configSchema.parse(rawConfig)
|
||||
const rawConfig = JSON.parse(fs.readFileSync("config.json", "utf-8"));
|
||||
const config = configSchema.parse(rawConfig);
|
||||
// config is fully typed: { database: { host: string, ... }, ... }
|
||||
|
||||
// API response validation
|
||||
const userSchema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
email: z.string().email(),
|
||||
})
|
||||
email: z.string().email()
|
||||
});
|
||||
|
||||
const response = await fetch('/api/user')
|
||||
const rawUser = await response.json()
|
||||
const user = userSchema.parse(rawUser)
|
||||
const response = await fetch("/api/user");
|
||||
const rawUser = await response.json();
|
||||
const user = userSchema.parse(rawUser);
|
||||
// user is fully typed and validated
|
||||
```
|
||||
|
||||
@@ -64,50 +64,53 @@ const user = userSchema.parse(rawUser)
|
||||
|
||||
```typescript
|
||||
function parseJSON<T>(schema: z.ZodType<T>, json: string): T {
|
||||
return schema.parse(JSON.parse(json))
|
||||
return schema.parse(JSON.parse(json));
|
||||
}
|
||||
|
||||
function safeParseJSON<T>(schema: z.ZodType<T>, json: string) {
|
||||
try {
|
||||
return { success: true as const, data: schema.parse(JSON.parse(json)) }
|
||||
return { success: true as const, data: schema.parse(JSON.parse(json)) };
|
||||
} catch (error) {
|
||||
if (error instanceof SyntaxError) {
|
||||
return { success: false as const, error: 'Invalid JSON' }
|
||||
return { success: false as const, error: "Invalid JSON" };
|
||||
}
|
||||
if (error instanceof z.ZodError) {
|
||||
return { success: false as const, error: error.issues }
|
||||
return { success: false as const, error: error.issues };
|
||||
}
|
||||
throw error
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Usage
|
||||
const config = parseJSON(configSchema, fs.readFileSync('config.json', 'utf-8'))
|
||||
const config = parseJSON(configSchema, fs.readFileSync("config.json", "utf-8"));
|
||||
```
|
||||
|
||||
**Validate localStorage/sessionStorage:**
|
||||
|
||||
```typescript
|
||||
const cartSchema = z.array(z.object({
|
||||
productId: z.string(),
|
||||
quantity: z.number().int().positive(),
|
||||
}))
|
||||
const cartSchema = z.array(
|
||||
z.object({
|
||||
productId: z.string(),
|
||||
quantity: z.number().int().positive()
|
||||
})
|
||||
);
|
||||
|
||||
function getCart() {
|
||||
const raw = localStorage.getItem('cart')
|
||||
if (!raw) return []
|
||||
const raw = localStorage.getItem("cart");
|
||||
if (!raw) return [];
|
||||
|
||||
const result = cartSchema.safeParse(JSON.parse(raw))
|
||||
const result = cartSchema.safeParse(JSON.parse(raw));
|
||||
if (!result.success) {
|
||||
// Corrupted cart data - clear it
|
||||
localStorage.removeItem('cart')
|
||||
return []
|
||||
localStorage.removeItem("cart");
|
||||
return [];
|
||||
}
|
||||
return result.data
|
||||
return result.data;
|
||||
}
|
||||
```
|
||||
|
||||
**When NOT to use this pattern:**
|
||||
|
||||
- When you genuinely need to pass through arbitrary JSON without processing
|
||||
|
||||
Reference: [Zod API - parse](https://zod.dev/api#parse)
|
||||
Reference in new issue
Block a user