From 9c083f2397ff1aafbe12e106f2fe2072d57ff3b2 Mon Sep 17 00:00:00 2001 From: Christian Fehmer Date: Sat, 7 Sep 2024 13:05:44 +0200 Subject: [PATCH] fix: unable to update profile with missing values (@fehmer) (#5859) --- .../__tests__/api/controllers/user.spec.ts | 44 +++++++++++++++++++ packages/contracts/src/schemas/users.ts | 10 ++--- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/backend/__tests__/api/controllers/user.spec.ts b/backend/__tests__/api/controllers/user.spec.ts index ddc596a65..ee37253bc 100644 --- a/backend/__tests__/api/controllers/user.spec.ts +++ b/backend/__tests__/api/controllers/user.spec.ts @@ -2879,6 +2879,50 @@ describe("user controller test", () => { } ); }); + it("should update with empty strings", async () => { + //GIVEN + const newProfile = { + bio: "", + keyboard: "", + + socialProfiles: { + github: "", + twitter: "", + website: "", + }, + }; + + //WHEN + const { body } = await mockApp + .patch("/users/profile") + .set("Authorization", `Uid ${uid}`) + .send({ + ...newProfile, + selectedBadgeId: -1, + }) + .expect(200); + + //THEN + expect(body).toEqual({ + message: "Profile updated", + data: newProfile, + }); + expect(updateProfileMock).toHaveBeenCalledWith( + uid, + { + bio: "", + keyboard: "", + socialProfiles: { + github: "", + twitter: "", + website: "", + }, + }, + { + badges: [{ id: 4 }, { id: 2 }, { id: 3 }], + } + ); + }); it("should fail with unknown properties", async () => { //WHEN const { body } = await mockApp diff --git a/packages/contracts/src/schemas/users.ts b/packages/contracts/src/schemas/users.ts index c7630ead7..9bcf3c417 100644 --- a/packages/contracts/src/schemas/users.ts +++ b/packages/contracts/src/schemas/users.ts @@ -109,8 +109,8 @@ function profileDetailsBase( export const UserProfileDetailsSchema = z .object({ - bio: profileDetailsBase(z.string().max(250)), - keyboard: profileDetailsBase(z.string().max(75)), + bio: profileDetailsBase(z.string().max(250)).or(z.literal("")), + keyboard: profileDetailsBase(z.string().max(75)).or(z.literal("")), socialProfiles: z .object({ twitter: profileDetailsBase( @@ -118,16 +118,16 @@ export const UserProfileDetailsSchema = z .string() .max(20) .regex(/^[0-9a-zA-Z_.-]+$/) - ), + ).or(z.literal("")), github: profileDetailsBase( z .string() .max(39) .regex(/^[0-9a-zA-Z_.-]+$/) - ), + ).or(z.literal("")), website: profileDetailsBase( z.string().url().max(200).startsWith("https://") - ), + ).or(z.literal("")), }) .strict() .optional(),