Skip to content

Commit c291c6c

Browse files
committed
Harden slot normalizer
1 parent b4f0bdb commit c291c6c

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

packages/core/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,12 +851,12 @@ const slotConfidenceSchema = z.object({
851851
});
852852

853853
export const rawSlotExtractionSchema = z.object({
854-
time: z.object({ hour: z.number().int(), minute: z.number().int() }).nullable(),
854+
time: z.object({ hour: z.number().int().min(0).max(23), minute: z.number().int().min(0).max(59) }).nullable(),
855855
day: z.object({
856856
kind: z.enum(["relative", "weekday", "absolute"]),
857857
value: z.string()
858858
}).nullable(),
859-
duration: z.object({ minutes: z.number().int() }).nullable(),
859+
duration: z.object({ minutes: z.number().int().min(0) }).nullable(),
860860
target: z.object({ entityId: z.string() }).nullable(),
861861
confidence: slotConfidenceSchema,
862862
unresolvable: z.array(slotKeySchema)

packages/core/src/slot-normalizer.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ describe("normalizeRawExtraction", () => {
4646
});
4747
});
4848

49+
it("drops time when hour is out of range", () => {
50+
expect(normalizeRawExtraction(extraction({ time: { hour: 25, minute: 0 } }))).toEqual({});
51+
});
52+
53+
it("drops time when minute is out of range", () => {
54+
expect(normalizeRawExtraction(extraction({ time: { hour: 10, minute: 70 } }))).toEqual({});
55+
});
56+
57+
it("drops time when hour is negative", () => {
58+
expect(normalizeRawExtraction(extraction({ time: { hour: -1, minute: 0 } }))).toEqual({});
59+
});
60+
61+
it("drops duration when minutes is negative", () => {
62+
expect(normalizeRawExtraction(extraction({ duration: { minutes: -30 } }))).toEqual({});
63+
});
64+
4965
it("returns empty object when no slots present", () => {
5066
expect(normalizeRawExtraction(extraction({}))).toEqual({});
5167
});

packages/core/src/slot-normalizer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import type { RawSlotExtraction, ResolvedSlots } from "./index";
33
export function normalizeRawExtraction(raw: RawSlotExtraction): Partial<ResolvedSlots> {
44
const result: Partial<ResolvedSlots> = {};
55

6-
if (raw.time) {
6+
if (raw.time && raw.time.hour >= 0 && raw.time.hour <= 23 && raw.time.minute >= 0 && raw.time.minute <= 59) {
77
result.time = `${String(raw.time.hour).padStart(2, "0")}:${String(raw.time.minute).padStart(2, "0")}`;
88
}
99

1010
if (raw.day) {
1111
result.day = raw.day.kind === "absolute" ? raw.day.value : raw.day.value.toLowerCase();
1212
}
1313

14-
if (raw.duration) {
14+
if (raw.duration && raw.duration.minutes >= 0) {
1515
result.duration = raw.duration.minutes;
1616
}
1717

0 commit comments

Comments
 (0)