@@ -11,15 +11,18 @@ import * as Loader from "../elements/loader";
1111import * as AccountButton from "../elements/account-button" ;
1212import { restart as restartTest } from "../test/test-logic" ;
1313import * as ChallengeController from "../controllers/challenge-controller" ;
14- import { Mode , Mode2 } from "@monkeytype/contracts/schemas/shared" ;
14+ import {
15+ DifficultySchema ,
16+ Mode2Schema ,
17+ ModeSchema ,
18+ } from "@monkeytype/contracts/schemas/shared" ;
1519import {
1620 CustomBackgroundFilter ,
1721 CustomBackgroundFilterSchema ,
1822 CustomBackgroundSize ,
1923 CustomBackgroundSizeSchema ,
2024 CustomThemeColors ,
2125 CustomThemeColorsSchema ,
22- Difficulty ,
2326} from "@monkeytype/contracts/schemas/configs" ;
2427import { z } from "zod" ;
2528import { parseWithSchema as parseJsonWithSchema } from "@monkeytype/util/json" ;
@@ -129,24 +132,35 @@ export function loadCustomThemeFromUrl(getOverride?: string): void {
129132 }
130133}
131134
132- type SharedTestSettings = [
133- Mode | null ,
134- Mode2 < Mode > | null ,
135- CustomText . CustomTextData | null ,
136- boolean | null ,
137- boolean | null ,
138- string | null ,
139- Difficulty | null ,
140- string | null
141- ] ;
135+ const TestSettingsSchema = z . tuple ( [
136+ ModeSchema . nullable ( ) ,
137+ Mode2Schema . nullable ( ) ,
138+ CustomText . CustomTextSettingsSchema . nullable ( ) ,
139+ z . boolean ( ) . nullable ( ) , //punctuation
140+ z . boolean ( ) . nullable ( ) , //numbers
141+ z . string ( ) . nullable ( ) , //language
142+ DifficultySchema . nullable ( ) ,
143+ z . string ( ) . nullable ( ) , //funbox
144+ ] ) ;
145+
146+ type SharedTestSettings = z . infer < typeof TestSettingsSchema > ;
142147
143148export function loadTestSettingsFromUrl ( getOverride ?: string ) : void {
144149 const getValue = Misc . findGetParameter ( "testSettings" , getOverride ) ;
145150 if ( getValue === null ) return ;
146151
147- const de = JSON . parse (
148- decompressFromURI ( getValue ) ?? ""
149- ) as SharedTestSettings ;
152+ let de : SharedTestSettings ;
153+ try {
154+ const decompressed = decompressFromURI ( getValue ) ?? "" ;
155+ de = parseJsonWithSchema ( decompressed , TestSettingsSchema ) ;
156+ } catch ( e ) {
157+ console . error ( "Failed to parse test settings:" , e ) ;
158+ Notifications . add (
159+ "Failed to load test settings from URL: " + ( e as Error ) . message ,
160+ 0
161+ ) ;
162+ return ;
163+ }
150164
151165 const applied : Record < string , string > = { } ;
152166
0 commit comments