1
Fork 0

Refactor: Replace username validation with schema from Tildes source code.

This replaces the simple >3 check with the correct min and max lengths and
Regex that are in the Tildes source code.
This commit is contained in:
Bauke 2019-11-10 23:28:51 +01:00
parent a3e52b627b
commit 68c374fee9
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
3 changed files with 15 additions and 3 deletions

View File

@ -192,6 +192,7 @@ function addLabelsToUsernames(settings: Settings): void {
const username: string = element
.textContent!.replace(/@/g, '')
.toLowerCase();
const addLabelSpan: HTMLSpanElement = createElementFromString(
`<span class="trx-user-label-add" data-trx-username="${username}">[+]</span>`
);

View File

@ -5,7 +5,8 @@ import {
log,
isColorBright,
getCurrentThemeKey,
querySelector
querySelector,
isValidTildesUsername
} from '../../utilities';
import {themeColors, ThemeKey} from '../../theme-colors';
@ -95,8 +96,8 @@ export function getLabelFormValues(): Except<UserLabel, 'id'> | undefined {
username: usernameInput.value.toLowerCase()
};
if (data.username.length < 3) {
log('No username was provided to add a label to.', true);
if (!isValidTildesUsername(data.username)) {
log(`Invalid Tildes username detected: ${data.username}`);
usernameInput.classList.add('trx-invalid');
return undefined;
}

View File

@ -252,3 +252,13 @@ export function flashMessage(message: string, error = false): void {
}
}, 5000);
}
// Validation copied from Tildes source code:
// https://gitlab.com/tildes/tildes/blob/master/tildes/tildes/schemas/user.py
export function isValidTildesUsername(username: string): boolean {
return (
username.length > 3 &&
username.length < 20 &&
/^[a-z0-9]([a-z0-9]|[_-](?![_-]))*[a-z0-9]$/i.exec(username) !== null
);
}