Skip to content

Commit 848732a

Browse files
committed
Fix validation for falsey values
1 parent 3dfa41b commit 848732a

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
Language,
33
ttlShutdownAt,
4-
validationSchema,
4+
getValidationSchema,
55
WorkspaceScheduleFormValues,
66
} from "./WorkspaceScheduleForm"
77
import { zones } from "./zones"
@@ -35,7 +35,7 @@ describe("validationSchema", () => {
3535
timezone: "",
3636
ttl: 0,
3737
}
38-
const validate = () => validationSchema.validateSync(values)
38+
const validate = () => getValidationSchema(true, true).validateSync(values)
3939
expect(validate).not.toThrow()
4040
})
4141

@@ -44,7 +44,7 @@ describe("validationSchema", () => {
4444
...valid,
4545
ttl: -1,
4646
}
47-
const validate = () => validationSchema.validateSync(values)
47+
const validate = () => getValidationSchema(true, true).validateSync(values)
4848
expect(validate).toThrow()
4949
})
5050

@@ -59,7 +59,7 @@ describe("validationSchema", () => {
5959
friday: false,
6060
saturday: false,
6161
}
62-
const validate = () => validationSchema.validateSync(values)
62+
const validate = () => getValidationSchema(true, true).validateSync(values)
6363
expect(validate).toThrowError(Language.errorNoDayOfWeek)
6464
})
6565

@@ -75,7 +75,7 @@ describe("validationSchema", () => {
7575
saturday: false,
7676
startTime: "",
7777
}
78-
const validate = () => validationSchema.validateSync(values)
78+
const validate = () => getValidationSchema(true, true).validateSync(values)
7979
expect(validate).toThrowError(Language.errorNoTime)
8080
})
8181

@@ -84,7 +84,7 @@ describe("validationSchema", () => {
8484
...valid,
8585
startTime: "16:20",
8686
}
87-
const validate = () => validationSchema.validateSync(values)
87+
const validate = () => getValidationSchema(true, true).validateSync(values)
8888
expect(validate).not.toThrow()
8989
})
9090

@@ -93,7 +93,7 @@ describe("validationSchema", () => {
9393
...valid,
9494
startTime: "9:30",
9595
}
96-
const validate = () => validationSchema.validateSync(values)
96+
const validate = () => getValidationSchema(true, true).validateSync(values)
9797
expect(validate).toThrowError(Language.errorTime)
9898
})
9999

@@ -102,7 +102,7 @@ describe("validationSchema", () => {
102102
...valid,
103103
startTime: "09:5",
104104
}
105-
const validate = () => validationSchema.validateSync(values)
105+
const validate = () => getValidationSchema(true, true).validateSync(values)
106106
expect(validate).toThrowError(Language.errorTime)
107107
})
108108

@@ -111,7 +111,7 @@ describe("validationSchema", () => {
111111
...valid,
112112
startTime: "24:01",
113113
}
114-
const validate = () => validationSchema.validateSync(values)
114+
const validate = () => getValidationSchema(true, true).validateSync(values)
115115
expect(validate).toThrowError(Language.errorTime)
116116
})
117117

@@ -120,7 +120,7 @@ describe("validationSchema", () => {
120120
...valid,
121121
startTime: "09:60",
122122
}
123-
const validate = () => validationSchema.validateSync(values)
123+
const validate = () => getValidationSchema(true, true).validateSync(values)
124124
expect(validate).toThrowError(Language.errorTime)
125125
})
126126

@@ -129,7 +129,7 @@ describe("validationSchema", () => {
129129
...valid,
130130
timezone: "Canada/North",
131131
}
132-
const validate = () => validationSchema.validateSync(values)
132+
const validate = () => getValidationSchema(true, true).validateSync(values)
133133
expect(validate).toThrowError(Language.errorTimezone)
134134
})
135135

@@ -138,7 +138,7 @@ describe("validationSchema", () => {
138138
...valid,
139139
timezone: zone,
140140
}
141-
const validate = () => validationSchema.validateSync(values)
141+
const validate = () => getValidationSchema(true, true).validateSync(values)
142142
expect(validate).not.toThrow()
143143
})
144144

@@ -147,7 +147,7 @@ describe("validationSchema", () => {
147147
...valid,
148148
ttl: 24 * 7,
149149
}
150-
const validate = () => validationSchema.validateSync(values)
150+
const validate = () => getValidationSchema(true, true).validateSync(values)
151151
expect(validate).not.toThrowError()
152152
})
153153

@@ -156,7 +156,7 @@ describe("validationSchema", () => {
156156
...valid,
157157
ttl: 24 * 7 + 1,
158158
}
159-
const validate = () => validationSchema.validateSync(values)
159+
const validate = () => getValidationSchema(true, true).validateSync(values)
160160
expect(validate).toThrowError("ttl must be less than or equal to 168")
161161
})
162162
})

site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.tsx

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { AutoStart } from "pages/WorkspaceSchedulePage/schedule"
2121
import { AutoStop } from "pages/WorkspaceSchedulePage/ttl"
2222
import { FC } from "react"
2323
import * as Yup from "yup"
24+
import { OptionalObjectSchema } from "yup/lib/object"
2425
import { getFormHelpersWithError } from "../../util/formUtils"
2526
import { FormFooter } from "../FormFooter/FormFooter"
2627
import { FullPageForm } from "../FullPageForm/FullPageForm"
@@ -36,10 +37,11 @@ dayjs.extend(relativeTime)
3637
dayjs.extend(timezone)
3738

3839
export const Language = {
39-
errorNoDayOfWeek: "Must set at least one day of week if start time is set",
40-
errorNoTime: "Start time is required when days of the week are selected",
40+
errorNoDayOfWeek: "Must set at least one day of week if auto-start is enabled",
41+
errorNoTime: "Start time is required when auto-start is enabled",
4142
errorTime: "Time must be in HH:mm format (24 hours)",
4243
errorTimezone: "Invalid timezone",
44+
errorNoStop: "Time until shutdown must be greater than zero when auto-stop is enabled",
4345
daysOfWeekLabel: "Days of Week",
4446
daySundayLabel: "Sunday",
4547
dayMondayLabel: "Monday",
@@ -89,12 +91,13 @@ export interface WorkspaceScheduleFormValues {
8991
ttl: number
9092
}
9193

92-
export const validationSchema = Yup.object({
94+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
95+
export const getValidationSchema = (autoStartEnabled: boolean, autoStopEnabled: boolean) => (Yup.object({
9396
sunday: Yup.boolean(),
9497
monday: Yup.boolean().test("at-least-one-day", Language.errorNoDayOfWeek, function (value) {
9598
const parent = this.parent as WorkspaceScheduleFormValues
9699

97-
if (!parent.startTime) {
100+
if (!autoStartEnabled) {
98101
return true
99102
} else {
100103
return ![
@@ -116,20 +119,8 @@ export const validationSchema = Yup.object({
116119

117120
startTime: Yup.string()
118121
.ensure()
119-
.test("required-if-day-selected", Language.errorNoTime, function (value) {
120-
const parent = this.parent as WorkspaceScheduleFormValues
121-
122-
const isDaySelected = [
123-
parent.sunday,
124-
parent.monday,
125-
parent.tuesday,
126-
parent.wednesday,
127-
parent.thursday,
128-
parent.friday,
129-
parent.saturday,
130-
].some((day) => day)
131-
132-
if (isDaySelected) {
122+
.test("required-if-auto-start", Language.errorNoTime, function (value) {
123+
if (autoStartEnabled) {
133124
return value !== ""
134125
} else {
135126
return true
@@ -168,9 +159,16 @@ export const validationSchema = Yup.object({
168159
}),
169160
ttl: Yup.number()
170161
.integer()
171-
.min(1)
172-
.max(24 * 7 /* 7 days */),
173-
})
162+
.min(0)
163+
.max(24 * 7 /* 7 days */)
164+
.test("positive-if-auto-stop", Language.errorNoStop, (value) => {
165+
if (autoStopEnabled) {
166+
return !!value
167+
} else {
168+
return true
169+
}
170+
}),
171+
}))
174172

175173
export const WorkspaceScheduleForm: FC<WorkspaceScheduleFormProps> = ({
176174
submitScheduleError,
@@ -190,7 +188,7 @@ export const WorkspaceScheduleForm: FC<WorkspaceScheduleFormProps> = ({
190188
initialValues,
191189
enableReinitialize: true,
192190
onSubmit,
193-
validationSchema,
191+
validationSchema: () => getValidationSchema(autoStart.enabled, autoStop.enabled),
194192
initialTouched,
195193
})
196194
const formHelpers = getFormHelpersWithError<WorkspaceScheduleFormValues>(

0 commit comments

Comments
 (0)