Skip to content

Commit f986572

Browse files
authored
impr(profanity): change error message when disallowed word is detected in username (@Leonabcd123) (#7766)
!nuf
1 parent 7c48be1 commit f986572

4 files changed

Lines changed: 29 additions & 25 deletions

File tree

backend/__tests__/api/controllers/user.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ describe("user controller test", () => {
193193
],
194194
});
195195
});
196-
it("should fail if username contains profanity", async () => {
196+
it("should fail if username contains disallowed word", async () => {
197197
//GIVEN
198198
const newUser = {
199199
uid: uid,
@@ -212,7 +212,7 @@ describe("user controller test", () => {
212212
expect(body).toEqual({
213213
message: "Invalid request data schema",
214214
validationErrors: [
215-
'"name" Profanity detected. Please remove it. If you believe this is a mistake, please contact us. (miodec)',
215+
'"name" Disallowed word detected. Please remove it. If you believe this is a mistake, please contact us (miodec).',
216216
],
217217
});
218218
});
@@ -277,7 +277,7 @@ describe("user controller test", () => {
277277
});
278278
expect(userIsNameAvailableMock).toHaveBeenCalledWith("bob", uid);
279279
});
280-
it("returns 422 if username contains profanity", async () => {
280+
it("returns 422 if username contains disallowed word", async () => {
281281
await mockApp
282282
.get("/users/checkName/newMiodec")
283283
//no authentication required
@@ -1139,7 +1139,7 @@ describe("user controller test", () => {
11391139
validationErrors: ["Unrecognized key(s) in object: 'extra'"],
11401140
});
11411141
});
1142-
it("should fail if username contains profanity", async () => {
1142+
it("should fail if username contains disallowed word", async () => {
11431143
//WHEN
11441144
const { body } = await mockApp
11451145
.patch("/users/name")
@@ -1151,7 +1151,7 @@ describe("user controller test", () => {
11511151
expect(body).toEqual({
11521152
message: "Invalid request data schema",
11531153
validationErrors: [
1154-
'"name" Profanity detected. Please remove it. If you believe this is a mistake, please contact us. (miodec)',
1154+
'"name" Disallowed word detected. Please remove it. If you believe this is a mistake, please contact us (miodec).',
11551155
],
11561156
});
11571157
});
@@ -3310,7 +3310,7 @@ describe("user controller test", () => {
33103310
expect.objectContaining({}),
33113311
);
33123312
});
3313-
it("should fail with profanity", async () => {
3313+
it("should fail with disallowed word", async () => {
33143314
//WHEN
33153315
const { body } = await mockApp
33163316
.patch("/users/profile")
@@ -3330,11 +3330,11 @@ describe("user controller test", () => {
33303330
expect(body).toEqual({
33313331
message: "Invalid request data schema",
33323332
validationErrors: [
3333-
'"bio" Profanity detected. Please remove it. If you believe this is a mistake, please contact us. (miodec)',
3334-
'"keyboard" Profanity detected. Please remove it. If you believe this is a mistake, please contact us. (miodec)',
3335-
'"socialProfiles.twitter" Profanity detected. Please remove it. If you believe this is a mistake, please contact us. (miodec)',
3336-
'"socialProfiles.github" Profanity detected. Please remove it. If you believe this is a mistake, please contact us. (miodec)',
3337-
'"socialProfiles.website" Profanity detected. Please remove it. If you believe this is a mistake, please contact us. (https://i-luv-miodec.com)',
3333+
'"bio" Disallowed word detected. Please remove it. If you believe this is a mistake, please contact us (miodec).',
3334+
'"keyboard" Disallowed word detected. Please remove it. If you believe this is a mistake, please contact us (miodec).',
3335+
'"socialProfiles.twitter" Disallowed word detected. Please remove it. If you believe this is a mistake, please contact us (miodec).',
3336+
'"socialProfiles.github" Disallowed word detected. Please remove it. If you believe this is a mistake, please contact us (miodec).',
3337+
'"socialProfiles.website" Disallowed word detected. Please remove it. If you believe this is a mistake, please contact us (https://i-luv-miodec.com).',
33383338
],
33393339
});
33403340
});

packages/contracts/__test__/validation/validation.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { describe, it, expect } from "vitest";
22
import * as Validation from "@monkeytype/schemas/validation/validation";
33

4+
const containsDisallowedWords = Validation.__testing.containsDisallowedWords;
5+
46
describe("validation", () => {
5-
it("containsProfanity", () => {
7+
it("containsDisallowedWords", () => {
68
const testCases = [
79
{
810
text: "https://www.fuckyou.com",
@@ -35,7 +37,7 @@ describe("validation", () => {
3537
];
3638

3739
testCases.forEach((testCase) => {
38-
expect(Validation.containsProfanity(testCase.text, "substring")).toBe(
40+
expect(containsDisallowedWords(testCase.text, "substring")).toBe(
3941
testCase.expected,
4042
);
4143
});

packages/schemas/src/users.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
PersonalBestSchema,
1313
} from "./shared";
1414
import { CustomThemeColorsSchema, FunboxNameSchema } from "./configs";
15-
import { doesNotContainProfanity } from "./validation/validation";
15+
import { doesNotContainDisallowedWords } from "./validation/validation";
1616
import { ConnectionSchema } from "./connections";
1717

1818
const NoneFilterSchema = z.literal("none");
@@ -85,7 +85,7 @@ export type UserTag = z.infer<typeof UserTagSchema>;
8585
function profileDetailsBase(
8686
schema: ZodString,
8787
): ZodEffects<ZodOptional<ZodEffects<ZodString>>> {
88-
return doesNotContainProfanity("word", schema)
88+
return doesNotContainDisallowedWords("word", schema)
8989
.optional()
9090
.transform((value) => (value === null ? undefined : value));
9191
}
@@ -242,7 +242,7 @@ export const FavoriteQuotesSchema = z.record(
242242
export type FavoriteQuotes = z.infer<typeof FavoriteQuotesSchema>;
243243

244244
export const UserEmailSchema = z.string().email();
245-
export const UserNameSchema = doesNotContainProfanity(
245+
export const UserNameSchema = doesNotContainDisallowedWords(
246246
"substring",
247247
z
248248
.string()

packages/schemas/src/validation/validation.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { replaceHomoglyphs } from "./homoglyphs";
22
import { ZodEffects, ZodString } from "zod";
33

44
// Sorry for the bad words
5-
const profanities = [
5+
const disallowedWords = [
66
"miodec",
77
"bitly",
88
"niqqa",
@@ -403,7 +403,7 @@ function sanitizeString(str: string | undefined): string | undefined {
403403
.replace(/\s{3,}/g, " ");
404404
}
405405

406-
export function containsProfanity(
406+
function containsDisallowedWords(
407407
text: string,
408408
mode: "word" | "substring",
409409
): boolean {
@@ -414,27 +414,29 @@ export function containsProfanity(
414414
return replaceHomoglyphs(sanitizeString(str) ?? "");
415415
});
416416

417-
const hasProfanity = profanities.some((profanity) => {
417+
const hasDisallowedWords = disallowedWords.some((disallowedWord) => {
418418
return normalizedText.some((word) => {
419419
return mode === "word"
420-
? word.startsWith(profanity)
421-
: word.includes(profanity);
420+
? word.startsWith(disallowedWord)
421+
: word.includes(disallowedWord);
422422
});
423423
});
424424

425-
return hasProfanity;
425+
return hasDisallowedWords;
426426
}
427427

428-
export function doesNotContainProfanity(
428+
export function doesNotContainDisallowedWords(
429429
mode: "word" | "substring",
430430
schema: ZodString,
431431
): ZodEffects<ZodString> {
432432
return schema.refine(
433433
(val) => {
434-
return !containsProfanity(val, mode);
434+
return !containsDisallowedWords(val, mode);
435435
},
436436
(val) => ({
437-
message: `Profanity detected. Please remove it. If you believe this is a mistake, please contact us. (${val})`,
437+
message: `Disallowed word detected. Please remove it. If you believe this is a mistake, please contact us (${val}).`,
438438
}),
439439
);
440440
}
441+
442+
export const __testing = { containsDisallowedWords };

0 commit comments

Comments
 (0)