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,70 +12,70 @@ When incoming data needs normalization before validation (trimming whitespace, p
|
||||
**Incorrect (validation fails on unnormalized data):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
const userSchema = z.object({
|
||||
name: z.string().min(1),
|
||||
email: z.string().email(),
|
||||
config: z.object({
|
||||
theme: z.string(),
|
||||
}),
|
||||
})
|
||||
theme: z.string()
|
||||
})
|
||||
});
|
||||
|
||||
// Raw form data
|
||||
const formData = {
|
||||
name: ' John Doe ', // Has whitespace
|
||||
email: 'JOHN@EXAMPLE.COM', // Uppercase
|
||||
config: '{"theme": "dark"}', // JSON string, not object
|
||||
}
|
||||
name: " John Doe ", // Has whitespace
|
||||
email: "JOHN@EXAMPLE.COM", // Uppercase
|
||||
config: '{"theme": "dark"}' // JSON string, not object
|
||||
};
|
||||
|
||||
userSchema.parse(formData)
|
||||
userSchema.parse(formData);
|
||||
// ZodError: Expected object, received string at "config"
|
||||
```
|
||||
|
||||
**Correct (using preprocess):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
// Preprocess normalizes before validation
|
||||
const trimmedString = z.preprocess(
|
||||
(val) => (typeof val === 'string' ? val.trim() : val),
|
||||
(val) => (typeof val === "string" ? val.trim() : val),
|
||||
z.string()
|
||||
)
|
||||
);
|
||||
|
||||
const lowercaseEmail = z.preprocess(
|
||||
(val) => (typeof val === 'string' ? val.toLowerCase().trim() : val),
|
||||
(val) => (typeof val === "string" ? val.toLowerCase().trim() : val),
|
||||
z.string().email()
|
||||
)
|
||||
);
|
||||
|
||||
const jsonObject = z.preprocess(
|
||||
(val) => {
|
||||
if (typeof val === 'string') {
|
||||
if (typeof val === "string") {
|
||||
try {
|
||||
return JSON.parse(val)
|
||||
return JSON.parse(val);
|
||||
} catch {
|
||||
return val // Let Zod report the error
|
||||
return val; // Let Zod report the error
|
||||
}
|
||||
}
|
||||
return val
|
||||
return val;
|
||||
},
|
||||
z.object({ theme: z.string() })
|
||||
)
|
||||
);
|
||||
|
||||
const userSchema = z.object({
|
||||
name: trimmedString.pipe(z.string().min(1)),
|
||||
email: lowercaseEmail,
|
||||
config: jsonObject,
|
||||
})
|
||||
config: jsonObject
|
||||
});
|
||||
|
||||
const formData = {
|
||||
name: ' John Doe ',
|
||||
email: 'JOHN@EXAMPLE.COM',
|
||||
config: '{"theme": "dark"}',
|
||||
}
|
||||
name: " John Doe ",
|
||||
email: "JOHN@EXAMPLE.COM",
|
||||
config: '{"theme": "dark"}'
|
||||
};
|
||||
|
||||
const user = userSchema.parse(formData)
|
||||
const user = userSchema.parse(formData);
|
||||
// { name: 'John Doe', email: 'john@example.com', config: { theme: 'dark' } }
|
||||
```
|
||||
|
||||
@@ -84,37 +84,31 @@ const user = userSchema.parse(formData)
|
||||
```typescript
|
||||
// Trim all strings
|
||||
const trimmedString = z.preprocess(
|
||||
(val) => (typeof val === 'string' ? val.trim() : val),
|
||||
(val) => (typeof val === "string" ? val.trim() : val),
|
||||
z.string()
|
||||
)
|
||||
);
|
||||
|
||||
// Parse numeric strings
|
||||
const numericString = z.preprocess(
|
||||
(val) => (typeof val === 'string' ? Number(val) : val),
|
||||
(val) => (typeof val === "string" ? Number(val) : val),
|
||||
z.number()
|
||||
)
|
||||
);
|
||||
|
||||
// Parse boolean-like values
|
||||
const booleanLike = z.preprocess(
|
||||
(val) => {
|
||||
if (val === 'true' || val === '1' || val === 1) return true
|
||||
if (val === 'false' || val === '0' || val === 0) return false
|
||||
return val
|
||||
},
|
||||
z.boolean()
|
||||
)
|
||||
const booleanLike = z.preprocess((val) => {
|
||||
if (val === "true" || val === "1" || val === 1) return true;
|
||||
if (val === "false" || val === "0" || val === 0) return false;
|
||||
return val;
|
||||
}, z.boolean());
|
||||
|
||||
// Parse date strings
|
||||
const dateString = z.preprocess(
|
||||
(val) => (typeof val === 'string' ? new Date(val) : val),
|
||||
z.date()
|
||||
)
|
||||
const dateString = z.preprocess((val) => (typeof val === "string" ? new Date(val) : val), z.date());
|
||||
|
||||
// Split comma-separated strings into arrays
|
||||
const csvArray = z.preprocess(
|
||||
(val) => (typeof val === 'string' ? val.split(',').map(s => s.trim()) : val),
|
||||
(val) => (typeof val === "string" ? val.split(",").map((s) => s.trim()) : val),
|
||||
z.array(z.string())
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
**Preprocess vs Transform:**
|
||||
@@ -122,11 +116,11 @@ const csvArray = z.preprocess(
|
||||
```typescript
|
||||
// preprocess() runs BEFORE type checking
|
||||
// Use for: Normalizing input format before validation
|
||||
z.preprocess(val => String(val).trim(), z.string().min(1))
|
||||
z.preprocess((val) => String(val).trim(), z.string().min(1));
|
||||
|
||||
// transform() runs AFTER type checking
|
||||
// Use for: Converting validated data to different format
|
||||
z.string().transform(s => s.toUpperCase())
|
||||
z.string().transform((s) => s.toUpperCase());
|
||||
|
||||
// Order of operations:
|
||||
// 1. preprocess receives raw unknown input
|
||||
@@ -135,6 +129,7 @@ z.string().transform(s => s.toUpperCase())
|
||||
```
|
||||
|
||||
**When NOT to use this pattern:**
|
||||
|
||||
- When `.coerce` methods handle the conversion (simpler)
|
||||
- When transformation should happen after validation (use transform)
|
||||
- When normalization could hide validation errors
|
||||
|
||||
Reference in new issue
Block a user