{"record":{"id":"4c3ff99c3a197c88","repo":"RocketChat/Rocket.Chat","slug":"error-input-is-not-a-valid-field-4c3ff9","errorCode":"error-input-is-not-a-valid-field","errorMessage":"${escape(userData.username)} is not a valid username","messagePattern":"(.+?) is not a valid username","errorType":"exception","errorClass":"MeteorError","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/users/saveUser/validateUserData.ts","lineNumber":79,"sourceCode":"\t}\n\n\tif (!isUpdateUserData(userData) && !trim(userData.username)) {\n\t\tthrow new MeteorError('error-the-field-is-required', 'The field Username is required', {\n\t\t\tmethod: 'insertOrUpdateUser',\n\t\t\tfield: 'Username',\n\t\t});\n\t}\n\n\tlet nameValidation;\n\n\ttry {\n\t\tnameValidation = new RegExp(`^${settings.get('UTF8_User_Names_Validation')}$`);\n\t} catch (e) {\n\t\tnameValidation = new RegExp('^[0-9a-zA-Z-_.]+$');\n\t}\n\n\tif (userData.username && !nameValidation.test(userData.username)) {\n\t\tthrow new MeteorError('error-input-is-not-a-valid-field', `${escape(userData.username)} is not a valid username`, {\n\t\t\tmethod: 'insertOrUpdateUser',\n\t\t\tinput: userData.username,\n\t\t\tfield: 'Username',\n\t\t});\n\t}\n\n\tif (!isUpdateUserData(userData) && !userData.password && !userData.setRandomPassword) {\n\t\tthrow new MeteorError('error-the-field-is-required', 'The field Password is required', {\n\t\t\tmethod: 'insertOrUpdateUser',\n\t\t\tfield: 'Password',\n\t\t});\n\t}\n\n\tif (!isUpdateUserData(userData)) {\n\t\tif (userData.username && !(await checkUsernameAvailability(userData.username))) {\n\t\t\tthrow new MeteorError('error-field-unavailable', `${escape(userData.username)} is already in use :(`, {\n\t\t\t\tmethod: 'insertOrUpdateUser',\n\t\t\t\tfield: userData.username,","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/users/saveUser/validateUserData.ts#L61-L97","documentation":"Thrown by validateUserData() when a submitted username fails the regex built from the UTF8_User_Names_Validation setting (escaped into ^...$; if the setting is an invalid regex the server falls back to ^[0-9a-zA-Z-_.]+$). The message interpolates the escaped username. It applies to creates AND updates whenever a username is present, and runs before availability checks.","triggerScenarios":"Username with spaces, accented/unicode characters, or symbols like '@' while UTF8_User_Names_Validation is the default ASCII class '[0-9a-zA-Z-_.]+'; admin widened/narrowed the setting and existing clients still validate against the old rule; a username that merely starts or ends with a character outside the class (the pattern is anchored ^...$).","commonSituations":"Workspace for non-Latin alphabets (e.g. Cyrillic or CJK usernames) without updating the setting; import scripts carrying over usernames from another system with different rules; trailing whitespace inside the username (trim only guards the required-check, and the class here does not include spaces).","solutions":["Match the client-side rule to the server setting: test the username against the same regex before submitting.","If unicode usernames should be allowed, set UTF8_User_Names_Validation (Administration -> General -> UTF8 or via PATCH /api/v1/settings/UTF8_User_Names_Validation) to a permissive class such as '[0-9a-zA-Z-_.\\p{L}]+' where supported.","Normalize the input: trim and strip characters outside the allowed class (commonly replacing spaces with dots or dashes)."],"exampleFix":"// before\nawait POST '/api/v1/users.create', { username: 'joão sousa', ... }); // throws with default ASCII validation\n\n// after (client-side guard + sanitized value)\nconst USERNAME_RE = /^[0-9a-zA-Z-_.]+$/;\nconst username = 'joão sousa'.normalize('NFKD').replace(/[^0-9a-zA-Z-_.]/g, '').slice(0, 22);\nif (!USERNAME_RE.test(username)) throw new Error('invalid username');\nawait POST '/api/v1/users.create', { username: 'joaosousa', ... });","handlingStrategy":"validation","validationCode":"const pattern = settings.get('UTF8_User_Names_Validation') ?? '[0-9a-zA-Z-_.]+';\nlet re: RegExp;\ntry { re = new RegExp(`^${pattern}$`); } catch { re = /^[0-9a-zA-Z-_.]+$/; }\nif (payload.username && !re.test(payload.username)) throw new Error('invalid username');","typeGuard":"const isValidUsername = (u: string, re: RegExp) => re.test(u);","tryCatchPattern":"catch (e) {\n  if (e.error === 'error-input-is-not-a-valid-field' && e.details?.field === 'Username') {\n    // sanitize (strip illegal chars) and resubmit once\n  }\n}","preventionTips":["Read UTF8_User_Names_Validation from public settings and reuse it client-side.","Test edge cases: unicode, spaces, leading/trailing punctuation.","Update the setting deliberately when enabling non-Latin usernames."],"tags":["users","username","validation","regex","settings","save-user"],"backgroundTag":"username-validation-failed","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}