Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion frontend/src/ts/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Config } from "@monkeytype/schemas/configs";
import { Mode, Mode2, PersonalBests } from "@monkeytype/schemas/shared";
import { Result } from "@monkeytype/schemas/results";
import { RankAndCount } from "@monkeytype/schemas/users";
import { SLUG_REGEX } from "@monkeytype/schemas/util";
import { roundTo2 } from "@monkeytype/util/numbers";
import { animate, AnimationParams } from "animejs";
import { ElementWithUtils } from "./dom";
Expand Down Expand Up @@ -151,7 +152,7 @@ export function isUsernameValid(name: string): boolean {
if (name.toLowerCase().includes("bitly")) return false;
if (name.length > 14) return false;
if (/^\..*/.test(name.toLowerCase())) return false;
return /^[0-9a-zA-Z_.-]+$/.test(name);
return SLUG_REGEX.test(name);
}

export function clearTimeouts(timeouts: (number | NodeJS.Timeout)[]): void {
Expand Down
4 changes: 2 additions & 2 deletions packages/schemas/src/ape-keys.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { z } from "zod";
import { IdSchema } from "./util";
import { IdSchema, SLUG_REGEX } from "./util";

export const ApeKeyNameSchema = z
.string()
.regex(/^[0-9a-zA-Z_.-]+$/)
.regex(SLUG_REGEX)
.max(20);

export const ApeKeyUserDefinedSchema = z.object({
Expand Down
4 changes: 2 additions & 2 deletions packages/schemas/src/presets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod";
import { IdSchema, TagSchema } from "./util";
import { IdSchema, NAME_REGEX, TagSchema } from "./util";
import {
ConfigGroupName,
ConfigGroupNameSchema,
Expand All @@ -8,7 +8,7 @@ import {

export const PresetNameSchema = z
.string()
.regex(/^[0-9a-zA-Z_-]+$/)
.regex(NAME_REGEX)
Comment thread
byseif21 marked this conversation as resolved.
Outdated
.max(16);
export type PresetName = z.infer<typeof PresetNameSchema>;

Expand Down
14 changes: 7 additions & 7 deletions packages/schemas/src/users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z, ZodEffects, ZodOptional, ZodString } from "zod";
import { IdSchema, StringNumberSchema } from "./util";
import { IdSchema, NAME_REGEX, SLUG_REGEX, StringNumberSchema } from "./util";
import { LanguageSchema } from "./languages";
import {
ModeSchema,
Expand All @@ -20,7 +20,7 @@ export const ResultFiltersSchema = z.object({
_id: IdSchema,
name: z
.string()
.regex(/^[0-9a-zA-Z_.-]+$/)
.regex(NAME_REGEX)
Comment thread
byseif21 marked this conversation as resolved.
Outdated
.max(16),
pb: z
.object({
Expand Down Expand Up @@ -94,14 +94,14 @@ export const TwitterProfileSchema = profileDetailsBase(
z
.string()
.max(20)
.regex(/^[0-9a-zA-Z_.-]+$/),
.regex(SLUG_REGEX),
).or(z.literal(""));

export const GithubProfileSchema = profileDetailsBase(
z
.string()
.max(39)
.regex(/^[0-9a-zA-Z_.-]+$/),
.regex(SLUG_REGEX),
).or(z.literal(""));

export const WebsiteSchema = profileDetailsBase(
Expand All @@ -127,7 +127,7 @@ export type UserProfileDetails = z.infer<typeof UserProfileDetailsSchema>;

export const CustomThemeNameSchema = z
.string()
.regex(/^[0-9a-zA-Z_-]+$/)
.regex(NAME_REGEX)
.max(16);
export type CustomThemeName = z.infer<typeof CustomThemeNameSchema>;

Expand Down Expand Up @@ -249,7 +249,7 @@ export const UserNameSchema = doesNotContainProfanity(
.min(1)
.max(16)
.regex(
/^[\da-zA-Z_-]+$/,
NAME_REGEX,
"Can only contain lower/uppercase letters, underscore and minus.",
Comment thread
byseif21 marked this conversation as resolved.
Outdated
),
);
Expand Down Expand Up @@ -299,7 +299,7 @@ export type ResultFiltersGroupItem<T extends ResultFiltersGroup> =

export const TagNameSchema = z
.string()
.regex(/^[0-9a-zA-Z_.-]+$/)
.regex(NAME_REGEX)
Comment thread
byseif21 marked this conversation as resolved.
Outdated
.max(16);
export type TagName = z.infer<typeof TagNameSchema>;

Expand Down
3 changes: 3 additions & 0 deletions packages/schemas/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export const StringNumberSchema = z
.or(z.number().transform(String));
export type StringNumber = z.infer<typeof StringNumberSchema>;

export const NAME_REGEX = /^[a-zA-Z0-9]+(?:[_-][a-zA-Z0-9]+)*$/;
export const SLUG_REGEX = /^[0-9a-zA-Z_.-]+$/;
Comment thread
byseif21 marked this conversation as resolved.
Outdated

export const token = (): ZodString => z.string().regex(/^[a-zA-Z0-9_]+$/);

export const IdSchema = token();
Expand Down
Loading