Skip to content

Commit

Permalink
Merge pull request #96 from shayypy/username-validation-warning
Browse files Browse the repository at this point in the history
Username validation warnings
  • Loading branch information
maddy committed Jul 7, 2023
2 parents e7403c3 + b7d7811 commit 2e0e67c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
15 changes: 15 additions & 0 deletions common/form/validators/isUsername.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const isUsername = () => {
return (value: string): string | false => {
for (const forbidden of ["discord", "```", "system message"]) {
if (value.toLowerCase().includes(forbidden)) {
return `Username cannot contain "${forbidden}"`
}
}
for (const forbidden of ["everyone", "here"]) {
if (value.toLowerCase() === forbidden) {
return `Username cannot be "${forbidden}"`
}
}
return false
}
}
3 changes: 2 additions & 1 deletion modules/editor/data/validation/isMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isNumber } from "./isNumber"
import { isShape } from "./isShape"
import { isString } from "./isString"
import { isUrl } from "./isUrl"
import { isUsername } from "./isUsername"
import { length } from "./length"
import { noExcessiveKeys } from "./noExcessiveKeys"
import { nullable } from "./nullable"
Expand All @@ -26,7 +27,7 @@ export const isMessage: Validator = first(
isShape({
content: optional(nullable(first(isString, length(1, 2000)))),
embeds: optional(nullable(first(contains(isEmbed), length(1, 10)))),
username: optional(first(isString, length(1, 256))),
username: optional(first(isUsername, length(1, 80))),
avatar_url: optional(isUrl),
thread_name: optional(first(isString, length(1, 100))),
flags: optional(isNumber),
Expand Down
11 changes: 11 additions & 0 deletions modules/editor/data/validation/isUsername.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { first } from "./first"
import { isString } from "./isString"
import type { Validator } from "./Validator"

export const isUsername: Validator = first(isString, (value, key) =>
!/(^(everyone|here)$)|discord|```|system message/i.test(value as string)
? []
: [
`${key}: Cannot contain "discord", "system message", or "\`\`\`", and cannot be "here" or "everyone"`,
],
)
3 changes: 2 additions & 1 deletion modules/message/state/editorForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
SubForm,
} from "mstform"
import { isUrl } from "../../../common/form/validators/isUrl"
import { isUsername } from "../../../common/form/validators/isUsername"
import { matchesRegex } from "../../../common/form/validators/matchesRegex"
import { maxLength } from "../../../common/form/validators/maxLength"
import { EditorManager, EditorManagerLike } from "../../editor/EditorManager"
Expand All @@ -21,7 +22,7 @@ export const editorForm = new Form(EditorManager, {
}),
username: new Field(converters.string, {
controlled: controlled.object,
validators: [maxLength(80)],
validators: [isUsername(), maxLength(80)],
}),
avatar: new Field(converters.string, {
controlled: controlled.object,
Expand Down

0 comments on commit 2e0e67c

Please sign in to comment.