mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
fix(validation): stop a future timestamp emitting a spurious past-bound error
Review follow-up to the shared entry-instant guard. The past-floor refine re-ran the full plausibility predicate, so a future-dated instant tripped both refines and the 422 carried a nonsensical "too far in the past" message alongside the correct future one. The floor refine now checks the past bound alone; a future value emits only the future message, matching the pre-refactor single-message contract. Also harden the Health-Score BP inputs and de-duplicate the tile-window derivation surfaced in the same review: - the prior-week graded score now keys off the prior-week window's own sufficiency rather than the current window's, so the week-over-week graded delta never pairs a confident current value against a thin-sample prior one; - the BD-Zielbereich 90-day count + effective span derive through one shared `deriveBpWindow90` helper, so the dashboard snapshot and the analytics thick slice can never compute the gate or the label span differently.
This commit is contained in:
1 parent
3e7aa49eb0
commit
3bb130d4a1
8 files changed
+99
-27
No files matched your search
@@ -87,4 +87,15 @@ describe("validateEntryInstant — Zod integration", () => {
|
||||
it("rejects an instant before 1900", () => {
|
||||
expect(schema.safeParse("1899-12-31T00:00:00.000Z").success).toBe(false);
|
||||
});
|
||||
|
||||
it("emits ONLY the future message for a future instant, not a spurious past-bound error", () => {
|
||||
const future = new Date(Date.now() + 60 * 60 * 1000).toISOString();
|
||||
const r = schema.safeParse(future);
|
||||
expect(r.success).toBe(false);
|
||||
if (!r.success) {
|
||||
const messages = r.error.issues.map((i) => i.message);
|
||||
expect(messages).toContain("Timestamp must not be in the future");
|
||||
expect(messages.some((m) => /past|1900/.test(m))).toBe(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -93,14 +93,24 @@ export function validateEntryInstant<T extends z.ZodType<Date>>(
|
||||
? "Timestamp is too far in the past"
|
||||
: "Timestamp must not predate 1900");
|
||||
return schema
|
||||
.refine((d) => d.getTime() <= Date.now() + ENTRY_INSTANT_CLOCK_SKEW_MS, {
|
||||
message: "Timestamp must not be in the future",
|
||||
})
|
||||
.refine(
|
||||
(d) =>
|
||||
d.getTime() <= Date.now() + ENTRY_INSTANT_CLOCK_SKEW_MS,
|
||||
{ message: "Timestamp must not be in the future" },
|
||||
)
|
||||
.refine(
|
||||
(d) =>
|
||||
isPlausibleEntryInstant(d, { ...bounds, now: Date.now() }),
|
||||
// Past-floor check ONLY — a future instant already fails the refine
|
||||
// above, and re-running the full plausibility predicate here would
|
||||
// make a future-dated value emit BOTH messages (the second being a
|
||||
// nonsensical "too far in the past"). Compare against the floor alone.
|
||||
(d) => d.getTime() >= pastFloorMs(bounds),
|
||||
{ message: pastMessage },
|
||||
) as T;
|
||||
}
|
||||
|
||||
/** Resolve the effective past floor (ms) for the given bounds at call time. */
|
||||
function pastFloorMs(bounds: Omit<EntryInstantBounds, "now">): number {
|
||||
if (bounds.maxAgeMs === undefined) return ENTRY_INSTANT_FAR_PAST.getTime();
|
||||
return Math.max(
|
||||
ENTRY_INSTANT_FAR_PAST.getTime(),
|
||||
Date.now() - bounds.maxAgeMs,
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user