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:
parent
a3e52b627b
commit
68c374fee9
|
@ -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>`
|
||||
);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue