Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Username validation warnings #96

Merged
merged 5 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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