Skip to content
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
4 changes: 2 additions & 2 deletions frontend/src/ts/commandline/lists/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isAuthenticated } from "../../states/core";
import { toggleFullscreen } from "../../utils/misc";
import { Command, withValidation } from "../types";
import { remoteValidation } from "../../utils/remote-validation";
import { UserNameSchema } from "@monkeytype/schemas/users";
import { UserNameWithoutFilterSchema } from "@monkeytype/schemas/users";
import Ape from "../../ape";

const commands: Command[] = [
Expand Down Expand Up @@ -60,7 +60,7 @@ const commands: Command[] = [
icon: "fa-search",
input: true,
validation: {
schema: UserNameSchema,
schema: UserNameWithoutFilterSchema,
debounceDelay: 1000,
isValid: remoteValidation(
async (name) => Ape.users.getProfile({ params: { uidOrName: name } }),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UserNameSchema } from "@monkeytype/schemas/users";
import { UserNameWithoutFilterSchema } from "@monkeytype/schemas/users";
import { createForm } from "@tanstack/solid-form";
import { createEffect, createSignal, JSXElement, Show } from "solid-js";

Expand Down Expand Up @@ -70,7 +70,7 @@ export function ProfileSearchPage(): JSXElement {
<form.Field
name="username"
validators={{
onChange: fromSchema(UserNameSchema),
onChange: fromSchema(UserNameWithoutFilterSchema),
onChangeAsyncDebounceMs: 1000,
onChangeAsync: async (field) => {
try {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/ts/pages/friends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { getAuthenticatedUser } from "../firebase";
import * as ServerConfiguration from "../ape/server-configuration";
import { authEvent } from "../events/auth";
import { Connection } from "@monkeytype/schemas/connections";
import { Friend, UserNameSchema } from "@monkeytype/schemas/users";
import { UserNameWithoutFilterSchema, Friend } from "@monkeytype/schemas/users";

import { showLoaderBar, hideLoaderBar } from "../states/loader-bar";
import { LocalStorageWithSchema } from "../utils/local-storage-with-schema";
Expand All @@ -52,7 +52,7 @@ const addFriendModal = new SimpleModal({
type: "text",
initVal: "",
validation: {
schema: UserNameSchema,
schema: UserNameWithoutFilterSchema,
isValid: remoteValidation(
async (name) => Ape.users.getNameAvailability({ params: { name } }),
{ check: (data) => !data.available || "Unknown user" },
Expand Down
7 changes: 1 addition & 6 deletions frontend/src/ts/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { lastElementFromArray } from "./arrays";
import { Config } from "@monkeytype/schemas/configs";
import { Mode, Mode2, PersonalBests } from "@monkeytype/schemas/shared";
import { Result } from "@monkeytype/schemas/results";
import { RankAndCount, UserNameSchema } from "@monkeytype/schemas/users";
import { RankAndCount } from "@monkeytype/schemas/users";
import { roundTo2 } from "@monkeytype/util/numbers";
import { animate, AnimationParams } from "animejs";
import { ElementWithUtils } from "./dom";
Expand Down Expand Up @@ -145,11 +145,6 @@ export function escapeHTML<T extends string | null | undefined>(str: T): T {
return str.replace(/[&<>"'/`]/g, (char) => escapeMap[char] as string) as T;
}

export function isUsernameValid(name: string): boolean {
if (name === null || name === undefined || name === "") return false;
return UserNameSchema.safeParse(name).success;
}

export function clearTimeouts(timeouts: (number | NodeJS.Timeout)[]): void {
timeouts.forEach((to) => {
if (typeof to === "number") clearTimeout(to);
Expand Down
11 changes: 10 additions & 1 deletion packages/schemas/src/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,18 @@ export const FavoriteQuotesSchema = z.record(
export type FavoriteQuotes = z.infer<typeof FavoriteQuotesSchema>;

export const UserEmailSchema = z.string().email();

/**
* username schema without profanity check
*/
export const UserNameWithoutFilterSchema = slug().min(1).max(16);

/**
* username schema with profanity check
*/
export const UserNameSchema = doesNotContainDisallowedWords(
"substring",
slug().min(1).max(16),
UserNameWithoutFilterSchema,
);

export const UserSchema = z.object({
Expand Down
Loading