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
@@ -12,69 +12,73 @@ When validating nested objects or arrays, `issue.path` tells you exactly where t
|
||||
**Incorrect (ignoring path information):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
const orderSchema = z.object({
|
||||
customer: z.object({
|
||||
name: z.string().min(1, 'Name required'),
|
||||
name: z.string().min(1, "Name required"),
|
||||
address: z.object({
|
||||
street: z.string().min(1, 'Street required'),
|
||||
city: z.string().min(1, 'City required'),
|
||||
}),
|
||||
street: z.string().min(1, "Street required"),
|
||||
city: z.string().min(1, "City required")
|
||||
})
|
||||
}),
|
||||
items: z.array(z.object({
|
||||
productId: z.string(),
|
||||
quantity: z.number().positive('Quantity must be positive'),
|
||||
})),
|
||||
})
|
||||
items: z.array(
|
||||
z.object({
|
||||
productId: z.string(),
|
||||
quantity: z.number().positive("Quantity must be positive")
|
||||
})
|
||||
)
|
||||
});
|
||||
|
||||
const result = orderSchema.safeParse({
|
||||
customer: { name: '', address: { street: '', city: '' } },
|
||||
items: [{ productId: 'abc', quantity: -1 }],
|
||||
})
|
||||
customer: { name: "", address: { street: "", city: "" } },
|
||||
items: [{ productId: "abc", quantity: -1 }]
|
||||
});
|
||||
|
||||
if (!result.success) {
|
||||
// Only showing message, not WHERE the error is
|
||||
result.error.issues.forEach(issue => {
|
||||
console.log(issue.message) // 'Name required', 'Street required', 'Quantity must be positive'
|
||||
result.error.issues.forEach((issue) => {
|
||||
console.log(issue.message); // 'Name required', 'Street required', 'Quantity must be positive'
|
||||
// User: "Which quantity? Which field?"
|
||||
})
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (using path information):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
const orderSchema = z.object({
|
||||
customer: z.object({
|
||||
name: z.string().min(1, 'Name required'),
|
||||
name: z.string().min(1, "Name required"),
|
||||
address: z.object({
|
||||
street: z.string().min(1, 'Street required'),
|
||||
city: z.string().min(1, 'City required'),
|
||||
}),
|
||||
street: z.string().min(1, "Street required"),
|
||||
city: z.string().min(1, "City required")
|
||||
})
|
||||
}),
|
||||
items: z.array(z.object({
|
||||
productId: z.string(),
|
||||
quantity: z.number().positive('Quantity must be positive'),
|
||||
})),
|
||||
})
|
||||
items: z.array(
|
||||
z.object({
|
||||
productId: z.string(),
|
||||
quantity: z.number().positive("Quantity must be positive")
|
||||
})
|
||||
)
|
||||
});
|
||||
|
||||
const result = orderSchema.safeParse({
|
||||
customer: { name: '', address: { street: '', city: '' } },
|
||||
items: [{ productId: 'abc', quantity: -1 }],
|
||||
})
|
||||
customer: { name: "", address: { street: "", city: "" } },
|
||||
items: [{ productId: "abc", quantity: -1 }]
|
||||
});
|
||||
|
||||
if (!result.success) {
|
||||
result.error.issues.forEach(issue => {
|
||||
result.error.issues.forEach((issue) => {
|
||||
// path is an array of keys/indices
|
||||
console.log(`${issue.path.join('.')}: ${issue.message}`)
|
||||
console.log(`${issue.path.join(".")}: ${issue.message}`);
|
||||
// 'customer.name: Name required'
|
||||
// 'customer.address.street: Street required'
|
||||
// 'customer.address.city: City required'
|
||||
// 'items.0.quantity: Quantity must be positive'
|
||||
})
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
@@ -82,33 +86,33 @@ if (!result.success) {
|
||||
|
||||
```typescript
|
||||
function mapErrorsToFields(error: z.ZodError) {
|
||||
const fieldErrors: Map<string, string[]> = new Map()
|
||||
const fieldErrors: Map<string, string[]> = new Map();
|
||||
|
||||
for (const issue of error.issues) {
|
||||
const fieldPath = issue.path.join('.')
|
||||
const existing = fieldErrors.get(fieldPath) ?? []
|
||||
fieldErrors.set(fieldPath, [...existing, issue.message])
|
||||
const fieldPath = issue.path.join(".");
|
||||
const existing = fieldErrors.get(fieldPath) ?? [];
|
||||
fieldErrors.set(fieldPath, [...existing, issue.message]);
|
||||
}
|
||||
|
||||
return fieldErrors
|
||||
return fieldErrors;
|
||||
}
|
||||
|
||||
// Usage
|
||||
const errors = mapErrorsToFields(result.error)
|
||||
errors.get('customer.name') // ['Name required']
|
||||
errors.get('items.0.quantity') // ['Quantity must be positive']
|
||||
const errors = mapErrorsToFields(result.error);
|
||||
errors.get("customer.name"); // ['Name required']
|
||||
errors.get("items.0.quantity"); // ['Quantity must be positive']
|
||||
```
|
||||
|
||||
**For array items, get index from path:**
|
||||
|
||||
```typescript
|
||||
const itemsWithErrors: Set<number> = new Set()
|
||||
const itemsWithErrors: Set<number> = new Set();
|
||||
|
||||
result.error.issues.forEach(issue => {
|
||||
if (issue.path[0] === 'items' && typeof issue.path[1] === 'number') {
|
||||
itemsWithErrors.add(issue.path[1])
|
||||
result.error.issues.forEach((issue) => {
|
||||
if (issue.path[0] === "items" && typeof issue.path[1] === "number") {
|
||||
itemsWithErrors.add(issue.path[1]);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Highlight items at indices: Set { 0 }
|
||||
```
|
||||
@@ -116,14 +120,15 @@ result.error.issues.forEach(issue => {
|
||||
**Using path with format():**
|
||||
|
||||
```typescript
|
||||
const formatted = result.error.format()
|
||||
const formatted = result.error.format();
|
||||
|
||||
// Access errors at any path level
|
||||
formatted.customer?.address?.city?._errors // ['City required']
|
||||
formatted.items?.[0]?.quantity?._errors // ['Quantity must be positive']
|
||||
formatted.customer?.address?.city?._errors; // ['City required']
|
||||
formatted.items?.[0]?.quantity?._errors; // ['Quantity must be positive']
|
||||
```
|
||||
|
||||
**When NOT to use this pattern:**
|
||||
|
||||
- Flat objects where field name is obvious
|
||||
- When using form libraries that handle path mapping
|
||||
|
||||
|
||||
Reference in new issue
Block a user