mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-29 23:58:06 +01:00
a5494fc637
* Fix account details editable label * Fix font-weight of the password label * Remove duplicate error * Hide password warning on first screen after bad password, autofocus password field * Fix jest failure * Restore className
44 lines
1.4 KiB
JavaScript
44 lines
1.4 KiB
JavaScript
import { INVISIBLE_CHARACTER } from '../../components/component-library/text/deprecated';
|
|
|
|
export function getAccountNameErrorMessage(
|
|
accounts,
|
|
context,
|
|
newAccountName,
|
|
defaultAccountName,
|
|
) {
|
|
const isDuplicateAccountName = accounts.some(
|
|
(item) => item.name === newAccountName,
|
|
);
|
|
|
|
const isEmptyAccountName = newAccountName === '';
|
|
|
|
const localizedWordForAccount = context
|
|
.t('newAccountNumberName')
|
|
.replace(' $1', '');
|
|
|
|
// Match strings starting with ${localizedWordForAccount} and then any numeral, case insensitive
|
|
// Trim spaces before and after
|
|
const reservedRegEx = new RegExp(
|
|
`^\\s*${localizedWordForAccount} \\d+\\s*$`,
|
|
'iu',
|
|
);
|
|
const isReservedAccountName = reservedRegEx.test(newAccountName);
|
|
|
|
const isValidAccountName =
|
|
newAccountName === defaultAccountName || // What is written in the text field is the same as the placeholder
|
|
(!isDuplicateAccountName && !isReservedAccountName && !isEmptyAccountName);
|
|
|
|
let errorMessage;
|
|
if (isValidAccountName) {
|
|
errorMessage = INVISIBLE_CHARACTER; // Using an invisible character, so the spacing stays constant
|
|
} else if (isDuplicateAccountName) {
|
|
errorMessage = context.t('accountNameDuplicate');
|
|
} else if (isReservedAccountName) {
|
|
errorMessage = context.t('accountNameReserved');
|
|
} else if (isEmptyAccountName) {
|
|
errorMessage = context.t('required');
|
|
}
|
|
|
|
return { isValidAccountName, errorMessage };
|
|
}
|