From d2355b2796c353ff6ba8444ae88378d5bcae2fb2 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sun, 15 Jun 2025 23:15:37 -0400 Subject: [PATCH] UsernameSchema, FlagSchema (#1185) ## Description: Fix zod validation failure for flag codes that do not conform to the SafeString pattern. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [x] I understand that submitting code with bugs that could have been caught through manual testing blocks releases and new features for all contributors Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com> --- src/core/Schemas.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/core/Schemas.ts b/src/core/Schemas.ts index d3f0256ae..285c704a7 100644 --- a/src/core/Schemas.ts +++ b/src/core/Schemas.ts @@ -179,10 +179,13 @@ export const AttackIntentSchema = BaseIntentSchema.extend({ troops: z.number().nullable(), }); +export const UsernameSchema = SafeString; +export const FlagSchema = z.string().max(128).optional(); + export const SpawnIntentSchema = BaseIntentSchema.extend({ - flag: z.string().nullable(), type: z.literal("spawn"), - name: SafeString, + name: UsernameSchema, + flag: FlagSchema, playerType: PlayerTypeSchema, x: z.number(), y: z.number(), @@ -346,8 +349,8 @@ export const ServerPrestartMessageSchema = ServerBaseMessageSchema.extend({ export const PlayerSchema = z.object({ clientID: ID, - username: SafeString, - flag: SafeString.optional(), + username: UsernameSchema, + flag: FlagSchema, }); export const GameStartInfoSchema = z.object({ @@ -424,8 +427,8 @@ export const ClientJoinMessageSchema = z.object({ token: TokenSchema, // WARNING: PII gameID: ID, lastTurn: z.number(), // The last turn the client saw. - username: SafeString, - flag: SafeString.optional(), + username: UsernameSchema, + flag: FlagSchema, }); export const ClientMessageSchema = z.discriminatedUnion("type", [