{"record":{"id":"d01cbef0ec096f15","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-custom-field-value-d01cbe","errorCode":"error-invalid-custom-field-value","errorMessage":"error-invalid-custom-field-value","messagePattern":"error-invalid-custom-field-value","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/omnichannel/custom-fields.ts","lineNumber":72,"sourceCode":"\tvalue,\n\toverwrite,\n}: {\n\tkey: string;\n\tvalue: string;\n\toverwrite: boolean;\n\ttoken: string;\n}): Promise<number> {\n\tlivechatLogger.debug({ msg: 'Setting custom fields data for visitor with token', token });\n\n\tconst customField = await LivechatCustomField.findOneById(key);\n\tif (!customField) {\n\t\tthrow new Error('invalid-custom-field');\n\t}\n\n\tif (customField.regexp !== undefined && customField.regexp !== '') {\n\t\tconst regexp = new RegExp(customField.regexp);\n\t\tif (!regexp.test(value)) {\n\t\t\tthrow new Error(i18n.t('error-invalid-custom-field-value', { field: key }));\n\t\t}\n\t}\n\n\tlet result;\n\tif (customField.scope === 'room') {\n\t\tresult = await LivechatRooms.updateDataByToken(token, key, value, overwrite);\n\t} else {\n\t\tresult = await LivechatVisitors.updateLivechatDataByToken(token, key, value, overwrite);\n\n\t\tconst visitor = await LivechatVisitors.getVisitorByToken(token, { projection: { _id: 1 } });\n\t\tif (visitor) {\n\t\t\tconst contacts = await LivechatContacts.findAllByVisitorId(visitor._id).toArray();\n\t\t\tif (contacts.length > 0) {\n\t\t\t\tawait Promise.all(contacts.map((contact) => updateContactsCustomFields(contact, [{ key, value, overwrite }])));\n\t\t\t}\n\t\t}\n\t}\n","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/omnichannel/custom-fields.ts#L54-L90","documentation":"setCustomFields found the field definition, and it has a non-empty regexp, but the submitted value does not match it. The thrown message is the i18n string 'error-invalid-custom-field-value' with the field key interpolated. This runs before the scope decision (room vs visitor), so nothing is written.","triggerScenarios":"POST /livechat/custom-field with a value violating the field's configured regexp — e.g. field 'email' with pattern validation receiving 'not-an-email', or a numeric id field receiving letters.","commonSituations":"Locale formatting (phones, dates), admin tightened a pattern after go-live, widget input lacks client-side masking.","solutions":["Fetch the field definition (GET /api/v1/livechat/custom-fields) and conform the value to its regexp","Fix an over-strict regexp in Administration > Omnichannel > Custom Fields if legitimate values fail","Pre-test the value with new RegExp(field.regexp) in the client and block submit early"],"exampleFix":"// before\n{ token, key: 'zip', value: 'ZIP 12345', overwrite: true } // regexp: /^[0-9]{5}$/\n\n// after\n{ token, key: 'zip', value: '12345', overwrite: true }","handlingStrategy":"validation","validationCode":"const definition = await LivechatCustomField.findOneById(key);\nif (definition?.regexp && !new RegExp(definition.regexp).test(value)) {\n  throw new Error(`Value for '${key}' does not match the field pattern`);\n}\nawait setCustomFields({ token, key, value, overwrite });","typeGuard":"const valueMatchesPattern = (value: string, regexp?: string): boolean =>\n  !regexp || regexp === '' || new RegExp(regexp).test(value);","tryCatchPattern":"try {\n  await setCustomFields({ token, key, value, overwrite });\n} catch (err) {\n  if (err instanceof Error && err.message.includes('error-invalid-custom-field-value')) {\n    // named field failed its regexp: conform the value and retry\n  }\n  throw err;\n}","preventionTips":["Fetch and cache field definitions (incl. regexp) in the widget and validate on input","Use unambiguous canonical formats (E.164 phones, ISO dates) in integrations","Alert admins when a regexp rejects a high share of real values"],"tags":["omnichannel","livechat","custom-fields","regexp","validation"],"backgroundTag":"regex-validation-failed","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}