backend/src/lib/validators/name.ts
creations 8a9499be85
All checks were successful
Code quality checks / biome (push) Successful in 8s
re-order alot, move to bun redis, generalized
2025-05-18 09:53:23 -04:00

31 lines
780 B
TypeScript

// ? should support non english characters but won't mess up the url
export const userNameRestrictions: {
length: { min: number; max: number };
regex: RegExp;
} = {
length: { min: 3, max: 20 },
regex: /^[\p{L}\p{N}._-]+$/u,
};
export function isValidUsername(username: string): {
valid: boolean;
error?: string;
} {
if (!username) {
return { valid: false, error: "" };
}
if (username.length < userNameRestrictions.length.min) {
return { valid: false, error: "Username is too short" };
}
if (username.length > userNameRestrictions.length.max) {
return { valid: false, error: "Username is too long" };
}
if (!userNameRestrictions.regex.test(username)) {
return { valid: false, error: "Username contains invalid characters" };
}
return { valid: true };
}