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,23 +12,25 @@ When using `.refine()` on object schemas for cross-field validation, add a `path
|
||||
**Incorrect (error at object level):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
const formSchema = z.object({
|
||||
password: z.string().min(8),
|
||||
confirmPassword: z.string(),
|
||||
}).refine(
|
||||
(data) => data.password === data.confirmPassword,
|
||||
{ message: 'Passwords do not match' } // No path specified
|
||||
)
|
||||
const formSchema = z
|
||||
.object({
|
||||
password: z.string().min(8),
|
||||
confirmPassword: z.string()
|
||||
})
|
||||
.refine(
|
||||
(data) => data.password === data.confirmPassword,
|
||||
{ message: "Passwords do not match" } // No path specified
|
||||
);
|
||||
|
||||
const result = formSchema.safeParse({
|
||||
password: 'secret123',
|
||||
confirmPassword: 'different',
|
||||
})
|
||||
password: "secret123",
|
||||
confirmPassword: "different"
|
||||
});
|
||||
|
||||
if (!result.success) {
|
||||
const flattened = result.error.flatten()
|
||||
const flattened = result.error.flatten();
|
||||
// {
|
||||
// formErrors: ['Passwords do not match'], // At form level!
|
||||
// fieldErrors: {} // Empty - no field association
|
||||
@@ -41,26 +43,25 @@ if (!result.success) {
|
||||
**Correct (error with path):**
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod'
|
||||
import { z } from "zod";
|
||||
|
||||
const formSchema = z.object({
|
||||
password: z.string().min(8),
|
||||
confirmPassword: z.string(),
|
||||
}).refine(
|
||||
(data) => data.password === data.confirmPassword,
|
||||
{
|
||||
message: 'Passwords do not match',
|
||||
path: ['confirmPassword'], // Error appears on this field
|
||||
}
|
||||
)
|
||||
const formSchema = z
|
||||
.object({
|
||||
password: z.string().min(8),
|
||||
confirmPassword: z.string()
|
||||
})
|
||||
.refine((data) => data.password === data.confirmPassword, {
|
||||
message: "Passwords do not match",
|
||||
path: ["confirmPassword"] // Error appears on this field
|
||||
});
|
||||
|
||||
const result = formSchema.safeParse({
|
||||
password: 'secret123',
|
||||
confirmPassword: 'different',
|
||||
})
|
||||
password: "secret123",
|
||||
confirmPassword: "different"
|
||||
});
|
||||
|
||||
if (!result.success) {
|
||||
const flattened = result.error.flatten()
|
||||
const flattened = result.error.flatten();
|
||||
// {
|
||||
// formErrors: [],
|
||||
// fieldErrors: {
|
||||
@@ -75,66 +76,73 @@ if (!result.success) {
|
||||
**Multiple cross-field validations:**
|
||||
|
||||
```typescript
|
||||
const dateRangeSchema = z.object({
|
||||
startDate: z.coerce.date(),
|
||||
endDate: z.coerce.date(),
|
||||
minDays: z.number().optional(),
|
||||
maxDays: z.number().optional(),
|
||||
}).refine(
|
||||
(data) => data.endDate >= data.startDate,
|
||||
{ message: 'End date must be after start date', path: ['endDate'] }
|
||||
).refine(
|
||||
(data) => {
|
||||
if (!data.minDays) return true
|
||||
const days = (data.endDate.getTime() - data.startDate.getTime()) / 86400000
|
||||
return days >= data.minDays
|
||||
},
|
||||
{ message: 'Date range is too short', path: ['endDate'] }
|
||||
).refine(
|
||||
(data) => {
|
||||
if (!data.maxDays) return true
|
||||
const days = (data.endDate.getTime() - data.startDate.getTime()) / 86400000
|
||||
return days <= data.maxDays
|
||||
},
|
||||
{ message: 'Date range is too long', path: ['endDate'] }
|
||||
)
|
||||
const dateRangeSchema = z
|
||||
.object({
|
||||
startDate: z.coerce.date(),
|
||||
endDate: z.coerce.date(),
|
||||
minDays: z.number().optional(),
|
||||
maxDays: z.number().optional()
|
||||
})
|
||||
.refine((data) => data.endDate >= data.startDate, {
|
||||
message: "End date must be after start date",
|
||||
path: ["endDate"]
|
||||
})
|
||||
.refine(
|
||||
(data) => {
|
||||
if (!data.minDays) return true;
|
||||
const days = (data.endDate.getTime() - data.startDate.getTime()) / 86400000;
|
||||
return days >= data.minDays;
|
||||
},
|
||||
{ message: "Date range is too short", path: ["endDate"] }
|
||||
)
|
||||
.refine(
|
||||
(data) => {
|
||||
if (!data.maxDays) return true;
|
||||
const days = (data.endDate.getTime() - data.startDate.getTime()) / 86400000;
|
||||
return days <= data.maxDays;
|
||||
},
|
||||
{ message: "Date range is too long", path: ["endDate"] }
|
||||
);
|
||||
```
|
||||
|
||||
**With superRefine for multiple path errors:**
|
||||
|
||||
```typescript
|
||||
const orderSchema = z.object({
|
||||
billingAddress: z.object({
|
||||
street: z.string(),
|
||||
city: z.string(),
|
||||
}),
|
||||
shippingAddress: z.object({
|
||||
street: z.string(),
|
||||
city: z.string(),
|
||||
}),
|
||||
sameAsBilling: z.boolean(),
|
||||
}).superRefine((data, ctx) => {
|
||||
if (data.sameAsBilling) {
|
||||
// If sameAsBilling but addresses differ, show errors on shipping
|
||||
if (data.shippingAddress.street !== data.billingAddress.street) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'Must match billing address',
|
||||
path: ['shippingAddress', 'street'], // Nested path
|
||||
})
|
||||
const orderSchema = z
|
||||
.object({
|
||||
billingAddress: z.object({
|
||||
street: z.string(),
|
||||
city: z.string()
|
||||
}),
|
||||
shippingAddress: z.object({
|
||||
street: z.string(),
|
||||
city: z.string()
|
||||
}),
|
||||
sameAsBilling: z.boolean()
|
||||
})
|
||||
.superRefine((data, ctx) => {
|
||||
if (data.sameAsBilling) {
|
||||
// If sameAsBilling but addresses differ, show errors on shipping
|
||||
if (data.shippingAddress.street !== data.billingAddress.street) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Must match billing address",
|
||||
path: ["shippingAddress", "street"] // Nested path
|
||||
});
|
||||
}
|
||||
if (data.shippingAddress.city !== data.billingAddress.city) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Must match billing address",
|
||||
path: ["shippingAddress", "city"]
|
||||
});
|
||||
}
|
||||
}
|
||||
if (data.shippingAddress.city !== data.billingAddress.city) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'Must match billing address',
|
||||
path: ['shippingAddress', 'city'],
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
```
|
||||
|
||||
**When NOT to use this pattern:**
|
||||
|
||||
- When the error genuinely applies to the whole object
|
||||
- Simple single-field refinements (path is implicit)
|
||||
|
||||
|
||||
Reference in new issue
Block a user