RocketChat/Rocket.Chat · error · Error

error-invalid-custom-field-value

error-invalid-custom-field-value

Error message

error-invalid-custom-field-value

What it means

Thrown by `saveGuest` while processing visitor custom fields: for each `LivechatCustomField` with scope 'visitor' present in `livechatData`, a non-empty value must match the field's stored `regexp`. The message is passed through `i18n.t('error-invalid-custom-field-value')`, so the user-visible text is translation-dependent while the code stays stable.

Source

Thrown at apps/meteor/server/lib/omnichannel/guests.ts:57

		...(name && { name }),
		...(email && { email }),
		...(phone && { phone }),
		livechatData: {},
	};

	const customFields: Record<string, any> = {};

	if ((!userId || (await hasPermissionAsync(userId, 'edit-livechat-room-customfields'))) && Object.keys(livechatData).length) {
		livechatLogger.debug({ msg: 'Saving custom fields for visitor', visitorId: _id, livechatData });
		for await (const field of LivechatCustomField.findByScope('visitor')) {
			if (!livechatData.hasOwnProperty(field._id)) {
				continue;
			}
			const value = trim(livechatData[field._id]);
			if (value !== '' && field.regexp !== undefined && field.regexp !== '') {
				const regexp = new RegExp(field.regexp);
				if (!regexp.test(value)) {
					throw new Error(i18n.t('error-invalid-custom-field-value'));
				}
			}
			customFields[field._id] = value;
		}
		updateData.livechatData = customFields;
		livechatLogger.debug({
			msg: 'About to update custom fields for visitor',
			visitorId: _id,
			customFieldCount: Object.keys(customFields).length,
		});
	}
	const ret = await LivechatVisitors.saveGuestById(_id, updateData);

	setImmediate(() => {
		void Apps.self?.triggerEvent(AppEvents.IPostLivechatGuestSaved, _id);
	});

	return ret;

View on GitHub (pinned to b2c16d5842)

Solutions

  1. Correct the submitted value so it matches the field's configured regexp (visible in Administration > Omnichannel > Custom Fields)
  2. If existing legitimate data now fails, relax or fix the custom field's regexp in admin
  3. Pre-validate each livechatData entry client-side against the field's regexp before calling saveGuest

Example fix

// before
await saveGuest({ _id, name, livechatData: { phone: 'call me' } }, userId);

// after
const phoneRe = new RegExp(field.regexp);
if (!phoneRe.test('call me')) {
  throw new Error(`phone must match ${field.regexp}`);
}
await saveGuest({ _id, name, livechatData: { phone: '+5511999999999' } }, userId);
Defensive patterns

Strategy: validation

Validate before calling

const fields = await LivechatCustomField.findByScope('visitor').toArray();
for (const [key, raw] of Object.entries(livechatData)) {
  const field = fields.find((f) => f._id === key);
  const value = String(raw).trim();
  if (value && field?.regexp && !new RegExp(field.regexp).test(value)) {
    throw new Error(`${key} must match ${field.regexp}`);
  }
}
await saveGuest(guestData, userId);

Type guard

const matchesCustomFieldRegex = (value: string, regexp?: string): boolean =>
  !regexp || regexp === '' || new RegExp(regexp).test(value);

Try / catch

try {
  await saveGuest(guestData, userId);
} catch (e) {
  if (e instanceof Error && e.message.includes('error-invalid-custom-field-value')) {
    // highlight the offending custom field in the form
  }
}

Prevention

When it happens

Trigger: Submitting visitor livechatData whose value fails the admin-configured regex — e.g. a 'Phone' field with regexp `^\+?[0-9]+$` receiving 'abc', or a field whose regexp was tightened after visitors already had free-text values saved.

Common situations: Regexes configured after data existed, breaking edits of old records; copy-pasted values with whitespace/emoji failing strict patterns; localized keyboards producing lookalike digits.

Related errors


AI-assisted analysis of RocketChat/Rocket.Chat@b2c16d5842 (2026-08-18). Data as JSON: /api/errors/1ce5ce4a1ac71104. Report an issue: GitHub.