Skip to content

Commit f60f59b

Browse files
committed
time validation
1 parent d61a332 commit f60f59b

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

site/src/components/WorkspaceStats/WorkspaceScheduleForm.test.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe("validationSchema", () => {
3232
})
3333

3434
it("disallows ttl to be negative", () => {
35-
const values = {
35+
const values: WorkspaceScheduleFormValues = {
3636
...valid,
3737
ttl: -1,
3838
}
@@ -41,7 +41,7 @@ describe("validationSchema", () => {
4141
})
4242

4343
it("disallows all days-of-week to be false when startTime is set", () => {
44-
const values = {
44+
const values: WorkspaceScheduleFormValues = {
4545
...valid,
4646
sunday: false,
4747
monday: false,
@@ -54,4 +54,40 @@ describe("validationSchema", () => {
5454
const validate = () => validationSchema.validateSync(values)
5555
expect(validate).toThrowError(Language.errorNoDayOfWeek)
5656
})
57+
58+
it("disallows startTime to be H:mm", () => {
59+
const values: WorkspaceScheduleFormValues = {
60+
...valid,
61+
startTime: "9:30",
62+
}
63+
const validate = () => validationSchema.validateSync(values)
64+
expect(validate).toThrowError(Language.errorTime)
65+
})
66+
67+
it("disallows startTime to be HH:m", () => {
68+
const values: WorkspaceScheduleFormValues = {
69+
...valid,
70+
startTime: "09:5",
71+
}
72+
const validate = () => validationSchema.validateSync(values)
73+
expect(validate).toThrowError(Language.errorTime)
74+
})
75+
76+
it("disallows an invalid startTime 13:01", () => {
77+
const values: WorkspaceScheduleFormValues = {
78+
...valid,
79+
startTime: "13:01",
80+
}
81+
const validate = () => validationSchema.validateSync(values)
82+
expect(validate).toThrowError(Language.errorTime)
83+
})
84+
85+
it("disallows an invalid startTime 09:60", () => {
86+
const values: WorkspaceScheduleFormValues = {
87+
...valid,
88+
startTime: "09:60",
89+
}
90+
const validate = () => validationSchema.validateSync(values)
91+
expect(validate).toThrowError(Language.errorTime)
92+
})
5793
})

site/src/components/WorkspaceStats/WorkspaceScheduleForm.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Stack } from "../Stack/Stack"
1616

1717
export const Language = {
1818
errorNoDayOfWeek: "Must set at least one day of week",
19+
errorTime: "Invalid time",
1920
daysOfWeekLabel: "Days of Week",
2021
daySundayLabel: "Sunday",
2122
dayMondayLabel: "Monday",
@@ -74,8 +75,20 @@ export const validationSchema = Yup.object({
7475
friday: Yup.boolean(),
7576
saturday: Yup.boolean(),
7677

77-
// TODO(Grey): Add validation that the string is "" or "HH:mm" (24 hours)
78-
startTime: Yup.string(),
78+
startTime: Yup.string()
79+
.ensure()
80+
.test("is-time-string", Language.errorTime, (value) => {
81+
if (value === "") {
82+
return true
83+
} else if (!/^[0-9][0-9]:[0-9][0-9]$/.test(value)) {
84+
return false
85+
} else {
86+
const parts = value.split(":")
87+
const HH = Number(parts[0])
88+
const mm = Number(parts[1])
89+
return HH >= 0 && HH <= 12 && mm >= 0 && mm <= 59
90+
}
91+
}),
7992
ttl: Yup.number().min(0).integer(),
8093
})
8194

0 commit comments

Comments
 (0)