Fix user validator preventing update

This commit is contained in:
Madeline 2022-09-27 23:30:46 +10:00
parent ff28c95f2a
commit add54e6b68

View File

@ -63,7 +63,7 @@ export const PrivateUserProjection = [
// Private user data that should never get sent to the client // Private user data that should never get sent to the client
export type PublicUser = Pick<User, PublicUserKeys>; export type PublicUser = Pick<User, PublicUserKeys>;
export interface UserPublic extends Pick<User, PublicUserKeys> {} export interface UserPublic extends Pick<User, PublicUserKeys> { }
export interface UserPrivate extends Pick<User, PrivateUserKeys> { export interface UserPrivate extends Pick<User, PrivateUserKeys> {
locale: string; locale: string;
@ -197,46 +197,57 @@ export class User extends BaseClass {
extended_settings: string; extended_settings: string;
@BeforeUpdate() @BeforeUpdate()
_update_validator() { this.validate(true); }
@BeforeInsert() @BeforeInsert()
validate() { _insert_validator() { this.validate(false); }
this.email = adjustEmail(this.email);
if (!this.email)
throw FieldErrors({
email: { message: "Invalid email", code: "EMAIL_INVALID" },
});
if (!this.email.match(/([a-z\d.-]{3,})@([a-z\d.-]+).([a-z]{2,})/g))
throw FieldErrors({
email: { message: "Invalid email", code: "EMAIL_INVALID" },
});
const discrim = Number(this.discriminator); validate(update: boolean = false) {
if (this.discriminator.length > 4) // inserting or email provided in update
throw FieldErrors({ if (!update || this.email) {
email: { this.email = adjustEmail(this.email);
message: "Discriminator cannot be more than 4 digits.", if (!this.email)
code: "DISCRIMINATOR_INVALID", throw FieldErrors({
}, email: { message: "Invalid email", code: "EMAIL_INVALID" },
}); });
if (isNaN(discrim)) if (!this.email.match(/([a-z\d.-]{3,})@([a-z\d.-]+).([a-z]{2,})/g))
throw FieldErrors({ throw FieldErrors({
email: { email: { message: "Invalid email", code: "EMAIL_INVALID" },
message: "Discriminator must be a number.", });
code: "DISCRIMINATOR_INVALID", }
},
});
if (discrim <= 0 || discrim >= 10000)
throw FieldErrors({
email: {
message: "Discriminator must be a number.",
code: "DISCRIMINATOR_INVALID",
},
});
this.discriminator = discrim.toString().padStart(4, "0");
if (BannedWords.find(this.username)) // inserting or discrim provided
throw FieldErrors({ if (!update || this.discriminator) {
username: { message: "Bad username", code: "INVALID_USERNAME" }, const discrim = Number(this.discriminator);
}); if (this.discriminator.length > 4)
throw FieldErrors({
email: {
message: "Discriminator cannot be more than 4 digits.",
code: "DISCRIMINATOR_INVALID",
},
});
if (isNaN(discrim))
throw FieldErrors({
email: {
message: "Discriminator must be a number.",
code: "DISCRIMINATOR_INVALID",
},
});
if (discrim <= 0 || discrim >= 10000)
throw FieldErrors({
email: {
message: "Discriminator must be a number.",
code: "DISCRIMINATOR_INVALID",
},
});
this.discriminator = discrim.toString().padStart(4, "0");
}
if (!update || this.username)
if (BannedWords.find(this.username))
throw FieldErrors({
username: { message: "Bad username", code: "INVALID_USERNAME" },
});
} }
toPublicUser() { toPublicUser() {
@ -369,7 +380,7 @@ export class User extends BaseClass {
setImmediate(async () => { setImmediate(async () => {
if (Config.get().guild.autoJoin.enabled) { if (Config.get().guild.autoJoin.enabled) {
for (const guild of Config.get().guild.autoJoin.guilds || []) { for (const guild of Config.get().guild.autoJoin.guilds || []) {
await Member.addToGuild(user.id, guild).catch((e) => {}); await Member.addToGuild(user.id, guild).catch((e) => { });
} }
} }
}); });
@ -436,7 +447,7 @@ export interface UserSettings {
disable_games_tab: boolean; disable_games_tab: boolean;
enable_tts_command: boolean; enable_tts_command: boolean;
explicit_content_filter: number; explicit_content_filter: number;
friend_source_flags: { all: boolean }; friend_source_flags: { all: boolean; };
gateway_connected: boolean; gateway_connected: boolean;
gif_auto_play: boolean; gif_auto_play: boolean;
// every top guild is displayed as a "folder" // every top guild is displayed as a "folder"