24 lines
668 B
TypeScript
24 lines
668 B
TypeScript
import * as errors from '@@/custom-errors';
|
|
|
|
export function validateUsername(username: string | undefined): string {
|
|
if (!username || !username.trim()) {
|
|
throw errors.BAD_REQUEST('Username is required');
|
|
}
|
|
|
|
const trimmed = username.trim();
|
|
|
|
if (trimmed.includes('@')) {
|
|
throw errors.BAD_REQUEST('Username cannot be an email address');
|
|
}
|
|
|
|
if (trimmed.length < 2 || trimmed.length > 32) {
|
|
throw errors.BAD_REQUEST('Username must be between 2 and 32 characters');
|
|
}
|
|
|
|
if (!/^[a-zA-Z0-9._-]+$/.test(trimmed)) {
|
|
throw errors.BAD_REQUEST('Username can only contain letters, numbers, dots, hyphens, and underscores');
|
|
}
|
|
|
|
return trimmed;
|
|
}
|