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,31 +12,33 @@ When using `.refine()` for custom validation, return `false` for invalid data in
|
||||
**Incorrect (throwing in refine):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
const passwordSchema = z.object({
|
||||
password: z.string().min(8),
|
||||
confirmPassword: z.string(),
|
||||
}).refine((data) => {
|
||||
if (data.password !== data.confirmPassword) {
|
||||
// Throwing stops all further validation
|
||||
throw new Error('Passwords do not match')
|
||||
}
|
||||
return true
|
||||
})
|
||||
const passwordSchema = z
|
||||
.object({
|
||||
password: z.string().min(8),
|
||||
confirmPassword: z.string()
|
||||
})
|
||||
.refine((data) => {
|
||||
if (data.password !== data.confirmPassword) {
|
||||
// Throwing stops all further validation
|
||||
throw new Error("Passwords do not match");
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
const formSchema = z.object({
|
||||
email: z.string().email(),
|
||||
passwords: passwordSchema,
|
||||
terms: z.boolean().refine((v) => v === true, 'Must accept terms'),
|
||||
})
|
||||
terms: z.boolean().refine((v) => v === true, "Must accept terms")
|
||||
});
|
||||
|
||||
// If passwords don't match, user never learns about other errors
|
||||
formSchema.safeParse({
|
||||
email: 'bad-email',
|
||||
passwords: { password: '12345678', confirmPassword: 'different' },
|
||||
terms: false,
|
||||
})
|
||||
email: "bad-email",
|
||||
passwords: { password: "12345678", confirmPassword: "different" },
|
||||
terms: false
|
||||
});
|
||||
// Only shows: "Passwords do not match"
|
||||
// Hidden: "Invalid email", "Must accept terms"
|
||||
```
|
||||
@@ -44,28 +46,30 @@ formSchema.safeParse({
|
||||
**Correct (returning false in refine):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
const passwordSchema = z.object({
|
||||
password: z.string().min(8),
|
||||
confirmPassword: z.string(),
|
||||
}).refine(
|
||||
(data) => data.password === data.confirmPassword,
|
||||
{ message: 'Passwords do not match', path: ['confirmPassword'] }
|
||||
)
|
||||
const passwordSchema = z
|
||||
.object({
|
||||
password: z.string().min(8),
|
||||
confirmPassword: z.string()
|
||||
})
|
||||
.refine((data) => data.password === data.confirmPassword, {
|
||||
message: "Passwords do not match",
|
||||
path: ["confirmPassword"]
|
||||
});
|
||||
|
||||
const formSchema = z.object({
|
||||
email: z.string().email(),
|
||||
passwords: passwordSchema,
|
||||
terms: z.boolean().refine((v) => v === true, 'Must accept terms'),
|
||||
})
|
||||
terms: z.boolean().refine((v) => v === true, "Must accept terms")
|
||||
});
|
||||
|
||||
// All errors are collected
|
||||
formSchema.safeParse({
|
||||
email: 'bad-email',
|
||||
passwords: { password: '12345678', confirmPassword: 'different' },
|
||||
terms: false,
|
||||
})
|
||||
email: "bad-email",
|
||||
passwords: { password: "12345678", confirmPassword: "different" },
|
||||
terms: false
|
||||
});
|
||||
// Shows all errors:
|
||||
// - "Invalid email"
|
||||
// - "Passwords do not match"
|
||||
@@ -80,47 +84,50 @@ const passwordSchema = z.string().superRefine((password, ctx) => {
|
||||
if (password.length < 8) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'Password must be at least 8 characters',
|
||||
})
|
||||
message: "Password must be at least 8 characters"
|
||||
});
|
||||
}
|
||||
|
||||
if (!/[A-Z]/.test(password)) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'Password must contain an uppercase letter',
|
||||
})
|
||||
message: "Password must contain an uppercase letter"
|
||||
});
|
||||
}
|
||||
|
||||
if (!/[0-9]/.test(password)) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'Password must contain a number',
|
||||
})
|
||||
message: "Password must contain a number"
|
||||
});
|
||||
}
|
||||
|
||||
// Don't return anything - issues are added via ctx
|
||||
})
|
||||
});
|
||||
|
||||
passwordSchema.safeParse('weak')
|
||||
passwordSchema.safeParse("weak");
|
||||
// All three errors reported at once
|
||||
```
|
||||
|
||||
**Correct pattern for async validation:**
|
||||
|
||||
```typescript
|
||||
const schema = z.object({
|
||||
email: z.string().email(),
|
||||
}).refine(
|
||||
async (data) => {
|
||||
// Return boolean, don't throw
|
||||
const exists = await checkEmailExists(data.email)
|
||||
return !exists
|
||||
},
|
||||
{ message: 'Email already registered', path: ['email'] }
|
||||
)
|
||||
const schema = z
|
||||
.object({
|
||||
email: z.string().email()
|
||||
})
|
||||
.refine(
|
||||
async (data) => {
|
||||
// Return boolean, don't throw
|
||||
const exists = await checkEmailExists(data.email);
|
||||
return !exists;
|
||||
},
|
||||
{ message: "Email already registered", path: ["email"] }
|
||||
);
|
||||
```
|
||||
|
||||
**When NOT to use this pattern:**
|
||||
|
||||
- When you need to abort validation entirely (security issues)
|
||||
- When subsequent validations depend on current check passing
|
||||
|
||||
|
||||
Reference in new issue
Block a user