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,21 +12,21 @@ By default, Zod objects use `.strip()` behavior, silently removing unrecognized
|
||||
**Default behavior (strip - silent removal):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
const userSchema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
})
|
||||
name: z.string()
|
||||
});
|
||||
|
||||
const input = {
|
||||
id: '123',
|
||||
name: 'John',
|
||||
role: 'admin', // Extra field
|
||||
secretToken: 'abc123', // Another extra field
|
||||
}
|
||||
id: "123",
|
||||
name: "John",
|
||||
role: "admin", // Extra field
|
||||
secretToken: "abc123" // Another extra field
|
||||
};
|
||||
|
||||
const user = userSchema.parse(input)
|
||||
const user = userSchema.parse(input);
|
||||
// { id: '123', name: 'John' }
|
||||
// Extra fields silently removed - was this intentional?
|
||||
```
|
||||
@@ -34,20 +34,22 @@ const user = userSchema.parse(input)
|
||||
**Using strict() to catch schema mismatches:**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
const userSchema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
}).strict()
|
||||
const userSchema = z
|
||||
.object({
|
||||
id: z.string(),
|
||||
name: z.string()
|
||||
})
|
||||
.strict();
|
||||
|
||||
const input = {
|
||||
id: '123',
|
||||
name: 'John',
|
||||
role: 'admin',
|
||||
}
|
||||
id: "123",
|
||||
name: "John",
|
||||
role: "admin"
|
||||
};
|
||||
|
||||
userSchema.parse(input)
|
||||
userSchema.parse(input);
|
||||
// ZodError: Unrecognized key(s) in object: 'role'
|
||||
|
||||
// This catches:
|
||||
@@ -60,49 +62,60 @@ userSchema.parse(input)
|
||||
|
||||
```typescript
|
||||
// strict() - Catch unexpected data (API contracts)
|
||||
const apiRequestSchema = z.object({
|
||||
action: z.string(),
|
||||
payload: z.unknown(),
|
||||
}).strict() // Fail if client sends unknown fields
|
||||
const apiRequestSchema = z
|
||||
.object({
|
||||
action: z.string(),
|
||||
payload: z.unknown()
|
||||
})
|
||||
.strict(); // Fail if client sends unknown fields
|
||||
|
||||
// strip() - Clean up data (explicit intention)
|
||||
const dbInsertSchema = z.object({
|
||||
name: z.string(),
|
||||
email: z.string(),
|
||||
}).strip() // Explicitly remove metadata before insert
|
||||
const dbInsertSchema = z
|
||||
.object({
|
||||
name: z.string(),
|
||||
email: z.string()
|
||||
})
|
||||
.strip(); // Explicitly remove metadata before insert
|
||||
|
||||
// passthrough() - Keep everything (pass-through proxy)
|
||||
const proxySchema = z.object({
|
||||
id: z.string(),
|
||||
}).passthrough() // Keep fields we don't validate
|
||||
const proxySchema = z
|
||||
.object({
|
||||
id: z.string()
|
||||
})
|
||||
.passthrough(); // Keep fields we don't validate
|
||||
|
||||
const input = { id: '123', extra: 'data' }
|
||||
proxySchema.parse(input) // { id: '123', extra: 'data' }
|
||||
const input = { id: "123", extra: "data" };
|
||||
proxySchema.parse(input); // { id: '123', extra: 'data' }
|
||||
```
|
||||
|
||||
**Choosing the right mode:**
|
||||
|
||||
| Mode | Behavior | Use When |
|
||||
|------|----------|----------|
|
||||
| `.strict()` | Reject unknown keys | API contracts, security-sensitive, debugging |
|
||||
| `.strip()` (default) | Remove unknown keys | General validation, data cleaning |
|
||||
| `.passthrough()` | Keep unknown keys | Proxying, partial validation |
|
||||
| Mode | Behavior | Use When |
|
||||
| -------------------- | ------------------- | -------------------------------------------- |
|
||||
| `.strict()` | Reject unknown keys | API contracts, security-sensitive, debugging |
|
||||
| `.strip()` (default) | Remove unknown keys | General validation, data cleaning |
|
||||
| `.passthrough()` | Keep unknown keys | Proxying, partial validation |
|
||||
|
||||
**Handling specific unknown keys:**
|
||||
|
||||
```typescript
|
||||
const schema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
}).catchall(z.unknown()) // Allow any additional fields of any type
|
||||
const schema = z
|
||||
.object({
|
||||
id: z.string(),
|
||||
name: z.string()
|
||||
})
|
||||
.catchall(z.unknown()); // Allow any additional fields of any type
|
||||
|
||||
// Or restrict additional fields to specific type
|
||||
const metadataSchema = z.object({
|
||||
id: z.string(),
|
||||
}).catchall(z.string()) // Only allow string extras
|
||||
const metadataSchema = z
|
||||
.object({
|
||||
id: z.string()
|
||||
})
|
||||
.catchall(z.string()); // Only allow string extras
|
||||
```
|
||||
|
||||
**When NOT to use this pattern:**
|
||||
|
||||
- `.strict()`: When forwarding data to another system that may add fields
|
||||
- `.passthrough()`: When you need to ensure only known fields are stored
|
||||
|
||||
|
||||
Reference in new issue
Block a user