Skip to content

Commit 2ee8d87

Browse files
committed
validation
1 parent adeb0ff commit 2ee8d87

File tree

2 files changed

+145
-7
lines changed

2 files changed

+145
-7
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Language, validationSchema, WorkspaceScheduleFormValues } from "./WorkspaceScheduleForm"
2+
3+
const valid: WorkspaceScheduleFormValues = {
4+
sunday: false,
5+
monday: true,
6+
tuesday: true,
7+
wednesday: true,
8+
thursday: true,
9+
friday: true,
10+
saturday: false,
11+
12+
startTime: "09:30",
13+
ttl: 120,
14+
}
15+
16+
describe("validationSchema", () => {
17+
it("allows everything to be falsy", () => {
18+
const values: WorkspaceScheduleFormValues = {
19+
sunday: false,
20+
monday: false,
21+
tuesday: false,
22+
wednesday: false,
23+
thursday: false,
24+
friday: false,
25+
saturday: false,
26+
27+
startTime: "",
28+
ttl: 0,
29+
}
30+
const validate = () => validationSchema.validateSync(values)
31+
expect(validate).not.toThrow()
32+
})
33+
34+
it("disallows ttl to be negative", () => {
35+
const values = {
36+
...valid,
37+
ttl: -1,
38+
}
39+
const validate = () => validationSchema.validateSync(values)
40+
expect(validate).toThrow()
41+
})
42+
43+
it("disallows all days-of-week to be false when startTime is set", () => {
44+
const values = {
45+
...valid,
46+
sunday: false,
47+
monday: false,
48+
tuesday: false,
49+
wednesday: false,
50+
thursday: false,
51+
friday: false,
52+
saturday: false,
53+
}
54+
const validate = () => validationSchema.validateSync(values)
55+
expect(validate).toThrowError(Language.errorNoDayOfWeek)
56+
})
57+
})

site/src/components/WorkspaceStats/WorkspaceScheduleForm.tsx

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import makeStyles from "@material-ui/core/styles/makeStyles"
77
import TextField from "@material-ui/core/TextField"
88
import { useFormik } from "formik"
99
import React from "react"
10+
import * as Yup from "yup"
1011
import { getFormHelpers } from "../../util/formUtils"
1112
import { FormFooter } from "../FormFooter/FormFooter"
1213
import { FullPageForm } from "../FullPageForm/FullPageForm"
1314
import { Stack } from "../Stack/Stack"
1415

1516
export const Language = {
17+
errorNoDayOfWeek: "Must set at least one day of week",
1618
daysOfWeekLabel: "Days of Week",
1719
daySundayLabel: "Sunday",
1820
dayMondayLabel: "Monday",
@@ -44,6 +46,35 @@ export interface WorkspaceScheduleFormValues {
4446
ttl: number
4547
}
4648

49+
export const validationSchema = Yup.object({
50+
sunday: Yup.boolean(),
51+
monday: Yup.boolean().test("at-least-one-day", Language.errorNoDayOfWeek, function (value) {
52+
const parent = this.parent as WorkspaceScheduleFormValues
53+
54+
if (!parent.startTime) {
55+
return true
56+
} else {
57+
return ![
58+
parent.sunday,
59+
value,
60+
parent.tuesday,
61+
parent.wednesday,
62+
parent.thursday,
63+
parent.friday,
64+
parent.saturday,
65+
].every((day) => day === false)
66+
}
67+
}),
68+
tuesday: Yup.boolean(),
69+
wednesday: Yup.boolean(),
70+
thursday: Yup.boolean(),
71+
friday: Yup.boolean(),
72+
saturday: Yup.boolean(),
73+
74+
startTime: Yup.string(),
75+
ttl: Yup.number().min(0).integer(),
76+
})
77+
4778
export const WorkspaceScheduleForm: React.FC<WorkspaceScheduleFormProps> = ({ onCancel, onSubmit }) => {
4879
const styles = useStyles()
4980

@@ -61,6 +92,7 @@ export const WorkspaceScheduleForm: React.FC<WorkspaceScheduleFormProps> = ({ on
6192
ttl: 0,
6293
},
6394
onSubmit,
95+
validationSchema,
6496
})
6597
const formHelpers = getFormHelpers<WorkspaceScheduleFormValues>(form)
6698

@@ -85,31 +117,80 @@ export const WorkspaceScheduleForm: React.FC<WorkspaceScheduleFormProps> = ({ on
85117

86118
<FormGroup>
87119
<FormControlLabel
88-
control={<Checkbox checked={form.values.sunday} onChange={form.handleChange} name="sunday" />}
120+
control={
121+
<Checkbox
122+
checked={form.values.sunday}
123+
disabled={!form.values.startTime}
124+
onChange={form.handleChange}
125+
name="sunday"
126+
/>
127+
}
89128
label={Language.daySundayLabel}
90129
/>
91130
<FormControlLabel
92-
control={<Checkbox checked={form.values.monday} onChange={form.handleChange} name="monday" />}
131+
control={
132+
<Checkbox
133+
checked={form.values.monday}
134+
disabled={!form.values.startTime}
135+
onChange={form.handleChange}
136+
name="monday"
137+
/>
138+
}
93139
label={Language.dayMondayLabel}
94140
/>
95141
<FormControlLabel
96-
control={<Checkbox checked={form.values.tuesday} onChange={form.handleChange} name="tuesday" />}
142+
control={
143+
<Checkbox
144+
checked={form.values.tuesday}
145+
disabled={!form.values.startTime}
146+
onChange={form.handleChange}
147+
name="tuesday"
148+
/>
149+
}
97150
label={Language.dayTuesdayLabel}
98151
/>
99152
<FormControlLabel
100-
control={<Checkbox checked={form.values.wednesday} onChange={form.handleChange} name="wednesday" />}
153+
control={
154+
<Checkbox
155+
checked={form.values.wednesday}
156+
disabled={!form.values.startTime}
157+
onChange={form.handleChange}
158+
name="wednesday"
159+
/>
160+
}
101161
label={Language.dayWednesdayLabel}
102162
/>
103163
<FormControlLabel
104-
control={<Checkbox checked={form.values.thursday} onChange={form.handleChange} name="thursday" />}
164+
control={
165+
<Checkbox
166+
checked={form.values.thursday}
167+
disabled={!form.values.startTime}
168+
onChange={form.handleChange}
169+
name="thursday"
170+
/>
171+
}
105172
label={Language.dayThursdayLabel}
106173
/>
107174
<FormControlLabel
108-
control={<Checkbox checked={form.values.friday} onChange={form.handleChange} name="friday" />}
175+
control={
176+
<Checkbox
177+
checked={form.values.friday}
178+
disabled={!form.values.startTime}
179+
onChange={form.handleChange}
180+
name="friday"
181+
/>
182+
}
109183
label={Language.dayFridayLabel}
110184
/>
111185
<FormControlLabel
112-
control={<Checkbox checked={form.values.saturday} onChange={form.handleChange} name="saturday" />}
186+
control={
187+
<Checkbox
188+
checked={form.values.saturday}
189+
disabled={!form.values.startTime}
190+
onChange={form.handleChange}
191+
name="saturday"
192+
/>
193+
}
113194
label={Language.daySaturdayLabel}
114195
/>
115196
</FormGroup>

0 commit comments

Comments
 (0)