Skip to content

Commit 83cc759

Browse files
authored
impr(commandline): validate input for polyglot and layoutfluid (@fehmer) (monkeytypegame#6709)
1 parent b9cff9e commit 83cc759

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

frontend/src/ts/commandline/lists.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,13 @@ import * as FPSCounter from "../elements/fps-counter";
101101
import {
102102
CustomBackgroundSchema,
103103
CustomLayoutFluid,
104+
CustomLayoutFluidSchema,
105+
CustomPolyglot,
106+
CustomPolyglotSchema,
104107
} from "@monkeytype/contracts/schemas/configs";
105-
import { Command, CommandsSubgroup } from "./types";
108+
import { Command, CommandsSubgroup, withValidation } from "./types";
106109
import * as TestLogic from "../test/test-logic";
107110
import * as ActivePage from "../states/active-page";
108-
import { Language } from "@monkeytype/contracts/schemas/languages";
109111

110112
const fontsPromise = JSONData.getFontsList();
111113
fontsPromise
@@ -189,37 +191,39 @@ export const commands: CommandsSubgroup = {
189191
...LanguagesCommands,
190192
...BritishEnglishCommands,
191193
...FunboxCommands,
192-
{
194+
withValidation({
193195
id: "changeCustomLayoutfluid",
194196
display: "Custom layoutfluid...",
195197
defaultValue: (): string => {
196198
return Config.customLayoutfluid.join(" ");
197199
},
198200
input: true,
199201
icon: "fa-tint",
202+
inputValueConvert: (val) => val.trim().split(" ") as CustomLayoutFluid,
203+
validation: { schema: CustomLayoutFluidSchema },
200204
exec: ({ input }): void => {
201205
if (input === undefined) return;
202-
UpdateConfig.setCustomLayoutfluid(
203-
input.split(" ") as CustomLayoutFluid
204-
);
206+
UpdateConfig.setCustomLayoutfluid(input);
205207
},
206-
},
207-
{
208+
}),
209+
withValidation({
208210
id: "changeCustomPolyglot",
209211
display: "Polyglot languages...",
210212
defaultValue: (): string => {
211213
return Config.customPolyglot.join(" ");
212214
},
213215
input: true,
214216
icon: "fa-language",
217+
inputValueConvert: (val) => val.trim().split(" ") as CustomPolyglot,
218+
validation: { schema: CustomPolyglotSchema },
215219
exec: ({ input }): void => {
216220
if (input === undefined) return;
217-
void UpdateConfig.setCustomPolyglot(input.split(" ") as Language[]);
221+
void UpdateConfig.setCustomPolyglot(input);
218222
if (ActivePage.get() === "test") {
219223
TestLogic.restart();
220224
}
221225
},
222-
},
226+
}),
223227

224228
//input
225229
...FreedomModeCommands,

packages/contracts/src/schemas/layouts.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { z } from "zod";
2+
import { customEnumErrorHandler } from "./util";
23

34
export const LayoutNameSchema = z.enum([
45
"qwerty",
@@ -217,7 +218,10 @@ export const LayoutNameSchema = z.enum([
217218
"tarmak_2",
218219
"tarmak_3",
219220
"tarmak_4",
220-
]
221+
],
222+
{
223+
errorMap: customEnumErrorHandler("Must be a supported layout"),
224+
}
221225
);
222226

223227
export type LayoutName = z.infer<typeof LayoutNameSchema>;

0 commit comments

Comments
 (0)