{"record":{"id":"fae109006338870d","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-username-fae109","errorCode":"error-invalid-username","errorMessage":"Invalid username","messagePattern":"Invalid username","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/users/checkUsernameAvailability.ts","lineNumber":25,"sourceCode":"import { settings } from '../../settings';\nimport type { UsernameAvailabilityCheckType } from '../callbacks/checkUsernameAvailabilityCallback';\nimport { checkUsernameAvailabilityCallback } from '../callbacks/checkUsernameAvailabilityCallback';\nimport { validateName } from '../shared/validateName';\n\nlet usernameBlackList: RegExp[] = [];\n\nconst toRegExp = (username: string): RegExp => new RegExp(`^${escapeRegExp(username).trim()}$`, 'i');\n\nsettings.watch('Accounts_BlockedUsernameList', (value: string) => {\n\tusernameBlackList = ['all', 'here'].concat(value.split(',')).map(toRegExp);\n});\n\nconst usernameIsBlocked = (username: string, usernameBlackList: RegExp[]): boolean | number =>\n\tusernameBlackList.length && usernameBlackList.some((restrictedUsername) => restrictedUsername.test(escapeRegExp(username).trim()));\n\nexport const checkUsernameAvailabilityWithValidation = async function (userId: string, username: string): Promise<boolean> {\n\tif (!username) {\n\t\tthrow new Meteor.Error('error-invalid-username', 'Invalid username', { method: 'setUsername' });\n\t}\n\n\tconst user = await Users.findOneById(userId, { projection: { username: 1 } });\n\n\tif (!user) {\n\t\tthrow new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'setUsername' });\n\t}\n\n\tif (user.username && !settings.get('Accounts_AllowUsernameChange')) {\n\t\tthrow new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'setUsername' });\n\t}\n\n\tif (user.username === username) {\n\t\treturn true;\n\t}\n\treturn checkUsernameAvailability(username);\n};\n","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/users/checkUsernameAvailability.ts#L7-L43","documentation":"checkUsernameAvailabilityWithValidation throws error-invalid-username (method 'setUsername') when the username argument is falsy: empty string, undefined, or null. This is the input-shape guard before any database lookup happens.","triggerScenarios":"Calling setUsername (or a flow that wraps this validation) with an empty form field, a variable that was never assigned, or a value that serializes to '' such as username.trim() of whitespace-only input.","commonSituations":"Profile form submitted with the username field cleared; onboarding step skipped; client sending undefined because the input state key is misspelled.","solutions":["Guard the form: require a non-empty trimmed username before invoking the method","Check for undefined/null earlier in the call chain (the guard only catches falsy values, not whitespace-only strings)","Disable the submit action while the field is empty"],"exampleFix":"// before\nMeteor.call('setUsername', usernameInput.value); // '' -> error-invalid-username\n\n// after\nconst username = usernameInput.value?.trim();\nif (!username) {\n  showFieldError('Username is required');\n} else {\n  Meteor.call('setUsername', username);\n}","handlingStrategy":"validation","validationCode":"const username = raw?.trim();\nif (!username) {\n  throw new Error('Username is required');\n}\nawait checkUsernameAvailabilityWithValidation(userId, username);","typeGuard":"const isNonEmptyUsername = (v: unknown): v is string => typeof v === 'string' && v.trim().length > 0;","tryCatchPattern":null,"preventionTips":["Disable submit while the username field is empty","Remember falsy-only guard: whitespace-only strings pass this check but fail later format validation"],"tags":["users","username","validation","input-validation"],"backgroundTag":"invalid-username","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}