v3: fixes to database schema and user registration
This commit is contained in:
@@ -89,16 +89,33 @@ export function isLoginUserRequest(obj: unknown): obj is LoginUserRequest {
|
||||
export function isRegisterUserRequest(
|
||||
obj: unknown,
|
||||
): obj is RegisterUserRequest {
|
||||
return validateRegisterUserRequest(obj) === null;
|
||||
}
|
||||
|
||||
/** Returns a human-readable error string, or null if the request is valid. */
|
||||
export function validateRegisterUserRequest(obj: unknown): string | null {
|
||||
if (
|
||||
!obj || typeof obj !== "object" ||
|
||||
!("username" in obj) || typeof obj.username !== "string" ||
|
||||
!("password" in obj) || typeof obj.password !== "string" ||
|
||||
!("inviteToken" in obj) || typeof obj.inviteToken !== "string"
|
||||
) return false;
|
||||
) return "Invalid request";
|
||||
const { username, password } = obj as RegisterUserRequest;
|
||||
return /^[a-zA-Z0-9_]{1,32}$/.test(username) &&
|
||||
password.length >= VALIDATION.PASSWORD_MIN &&
|
||||
password.length <= VALIDATION.PASSWORD_MAX;
|
||||
if (
|
||||
!new RegExp(
|
||||
`^[a-zA-Z0-9_]{${VALIDATION.USERNAME_MIN},${VALIDATION.USERNAME_MAX}}$`,
|
||||
)
|
||||
.test(username)
|
||||
) {
|
||||
return `Username must be ${VALIDATION.USERNAME_MIN}–${VALIDATION.USERNAME_MAX} characters and contain only letters, numbers, or underscores`;
|
||||
}
|
||||
if (password.length < VALIDATION.PASSWORD_MIN) {
|
||||
return `Password must be at least ${VALIDATION.PASSWORD_MIN} characters`;
|
||||
}
|
||||
if (password.length > VALIDATION.PASSWORD_MAX) {
|
||||
return `Password must be at most ${VALIDATION.PASSWORD_MAX} characters`;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function isUpdateUserRequest(obj: unknown): obj is UpdateUserRequest {
|
||||
@@ -310,7 +327,10 @@ export function isCreatePlaylistRequest(
|
||||
!("isPublic" in obj) || typeof obj.isPublic !== "boolean"
|
||||
) return false;
|
||||
const o = obj as Record<string, unknown>;
|
||||
if ((o.title as string).length === 0 || (o.title as string).length > VALIDATION.PLAYLIST_TITLE_MAX) return false;
|
||||
if (
|
||||
(o.title as string).length === 0 ||
|
||||
(o.title as string).length > VALIDATION.PLAYLIST_TITLE_MAX
|
||||
) return false;
|
||||
if (
|
||||
"description" in o && typeof o.description !== "string" &&
|
||||
o.description !== null
|
||||
@@ -329,7 +349,10 @@ export function isUpdatePlaylistRequest(
|
||||
const o = obj as Record<string, unknown>;
|
||||
if ("title" in o) {
|
||||
if (typeof o.title !== "string") return false;
|
||||
if ((o.title as string).length === 0 || (o.title as string).length > VALIDATION.PLAYLIST_TITLE_MAX) return false;
|
||||
if (
|
||||
(o.title as string).length === 0 ||
|
||||
(o.title as string).length > VALIDATION.PLAYLIST_TITLE_MAX
|
||||
) return false;
|
||||
}
|
||||
if (
|
||||
"description" in o && typeof o.description !== "string" &&
|
||||
|
||||
Reference in New Issue
Block a user