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,16 +12,16 @@ When data needs to pass through multiple validation stages (coerce string to num
|
||||
**Incorrect (unclear transformation chain):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
// All transforms in one long chain - hard to understand stages
|
||||
const priceSchema = z
|
||||
.string()
|
||||
.transform((s) => parseFloat(s.replace(/[$,]/g, '')))
|
||||
.refine((n) => !isNaN(n), 'Invalid number')
|
||||
.refine((n) => n >= 0, 'Must be positive')
|
||||
.refine((n) => n <= 1000000, 'Too large')
|
||||
.transform((n) => Math.round(n * 100))
|
||||
.transform((s) => parseFloat(s.replace(/[$,]/g, "")))
|
||||
.refine((n) => !isNaN(n), "Invalid number")
|
||||
.refine((n) => n >= 0, "Must be positive")
|
||||
.refine((n) => n <= 1000000, "Too large")
|
||||
.transform((n) => Math.round(n * 100));
|
||||
|
||||
// What type is n at each stage? Hard to tell
|
||||
```
|
||||
@@ -29,24 +29,24 @@ const priceSchema = z
|
||||
**Correct (using pipe for clear stages):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
// Stage 1: Coerce string to number
|
||||
const parsePrice = z.string().transform((s) => {
|
||||
const cleaned = s.replace(/[$,]/g, '')
|
||||
const parsed = parseFloat(cleaned)
|
||||
if (isNaN(parsed)) throw new Error('Invalid number')
|
||||
return parsed
|
||||
})
|
||||
const cleaned = s.replace(/[$,]/g, "");
|
||||
const parsed = parseFloat(cleaned);
|
||||
if (isNaN(parsed)) throw new Error("Invalid number");
|
||||
return parsed;
|
||||
});
|
||||
|
||||
// Stage 2: Validate number constraints
|
||||
const validPrice = z.number().min(0, 'Must be positive').max(1000000, 'Too large')
|
||||
const validPrice = z.number().min(0, "Must be positive").max(1000000, "Too large");
|
||||
|
||||
// Stage 3: Transform to cents
|
||||
const centsPrice = z.number().transform((n) => Math.round(n * 100))
|
||||
const centsPrice = z.number().transform((n) => Math.round(n * 100));
|
||||
|
||||
// Pipe them together - clear data flow
|
||||
const priceSchema = parsePrice.pipe(validPrice).pipe(centsPrice)
|
||||
const priceSchema = parsePrice.pipe(validPrice).pipe(centsPrice);
|
||||
|
||||
// Type at each stage is clear:
|
||||
// string -> number (parsePrice)
|
||||
@@ -58,12 +58,12 @@ const priceSchema = parsePrice.pipe(validPrice).pipe(centsPrice)
|
||||
|
||||
```typescript
|
||||
// Without pipe - validation runs on raw input
|
||||
const schema1 = z.coerce.number().min(1)
|
||||
schema1.parse('') // Passes! Empty string coerces to 0, but then... wait, 0 < 1
|
||||
const schema1 = z.coerce.number().min(1);
|
||||
schema1.parse(""); // Passes! Empty string coerces to 0, but then... wait, 0 < 1
|
||||
|
||||
// With pipe - validation runs on coerced value
|
||||
const schema2 = z.coerce.number().pipe(z.number().min(1))
|
||||
schema2.parse('') // Fails correctly: 0 is less than 1
|
||||
const schema2 = z.coerce.number().pipe(z.number().min(1));
|
||||
schema2.parse(""); // Fails correctly: 0 is less than 1
|
||||
```
|
||||
|
||||
**Complex data transformation:**
|
||||
@@ -75,7 +75,7 @@ schema2.parse('') // Fails correctly: 0 is less than 1
|
||||
const emailArraySchema = z
|
||||
.string()
|
||||
// Stage 1: Split CSV
|
||||
.transform((s) => s.split(',').map((e) => e.trim()))
|
||||
.transform((s) => s.split(",").map((e) => e.trim()))
|
||||
// Stage 2: Validate as email array
|
||||
.pipe(z.array(z.string().email()))
|
||||
// Stage 3: Transform to objects
|
||||
@@ -83,12 +83,12 @@ const emailArraySchema = z
|
||||
z.array(z.string()).transform((emails) =>
|
||||
emails.map((email) => ({
|
||||
address: email.toLowerCase(),
|
||||
domain: email.split('@')[1],
|
||||
domain: email.split("@")[1]
|
||||
}))
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
emailArraySchema.parse('John@Example.com, jane@test.com')
|
||||
emailArraySchema.parse("John@Example.com, jane@test.com");
|
||||
// [
|
||||
// { address: 'john@example.com', domain: 'Example.com' },
|
||||
// { address: 'jane@test.com', domain: 'test.com' }
|
||||
@@ -98,15 +98,16 @@ emailArraySchema.parse('John@Example.com, jane@test.com')
|
||||
**Type inference with pipe:**
|
||||
|
||||
```typescript
|
||||
const schema = z.string().pipe(z.coerce.number()).pipe(z.number().positive())
|
||||
const schema = z.string().pipe(z.coerce.number()).pipe(z.number().positive());
|
||||
|
||||
type Input = z.input<typeof schema> // string
|
||||
type Output = z.output<typeof schema> // number
|
||||
type Input = z.input<typeof schema>; // string
|
||||
type Output = z.output<typeof schema>; // number
|
||||
|
||||
// Each pipe stage has clear input/output types
|
||||
```
|
||||
|
||||
**When NOT to use this pattern:**
|
||||
|
||||
- Simple single-stage validation (adds unnecessary complexity)
|
||||
- When `.refine()` chain is sufficient and readable
|
||||
|
||||
|
||||
Reference in new issue
Block a user