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

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.

Source

Thrown at apps/meteor/server/lib/omnichannel/custom-fields.ts:72

	value,
	overwrite,
}: {
	key: string;
	value: string;
	overwrite: boolean;
	token: string;
}): Promise<number> {
	livechatLogger.debug({ msg: 'Setting custom fields data for visitor with token', token });

	const customField = await LivechatCustomField.findOneById(key);
	if (!customField) {
		throw new Error('invalid-custom-field');
	}

	if (customField.regexp !== undefined && customField.regexp !== '') {
		const regexp = new RegExp(customField.regexp);
		if (!regexp.test(value)) {
			throw new Error(i18n.t('error-invalid-custom-field-value', { field: key }));
		}
	}

	let result;
	if (customField.scope === 'room') {
		result = await LivechatRooms.updateDataByToken(token, key, value, overwrite);
	} else {
		result = await LivechatVisitors.updateLivechatDataByToken(token, key, value, overwrite);

		const visitor = await LivechatVisitors.getVisitorByToken(token, { projection: { _id: 1 } });
		if (visitor) {
			const contacts = await LivechatContacts.findAllByVisitorId(visitor._id).toArray();
			if (contacts.length > 0) {
				await Promise.all(contacts.map((contact) => updateContactsCustomFields(contact, [{ key, value, overwrite }])));
			}
		}
	}

View on GitHub (pinned to b2c16d5842)

Solutions

  1. Fetch the field definition (GET /api/v1/livechat/custom-fields) and conform the value to its regexp
  2. Fix an over-strict regexp in Administration > Omnichannel > Custom Fields if legitimate values fail
  3. Pre-test the value with new RegExp(field.regexp) in the client and block submit early

Example fix

// before
{ token, key: 'zip', value: 'ZIP 12345', overwrite: true } // regexp: /^[0-9]{5}$/

// after
{ token, key: 'zip', value: '12345', overwrite: true }
Defensive patterns

Strategy: validation

Validate before calling

const definition = await LivechatCustomField.findOneById(key);
if (definition?.regexp && !new RegExp(definition.regexp).test(value)) {
  throw new Error(`Value for '${key}' does not match the field pattern`);
}
await setCustomFields({ token, key, value, overwrite });

Type guard

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

Try / catch

try {
  await setCustomFields({ token, key, value, overwrite });
} catch (err) {
  if (err instanceof Error && err.message.includes('error-invalid-custom-field-value')) {
    // named field failed its regexp: conform the value and retry
  }
  throw err;
}

Prevention

When it happens

Trigger: 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.

Common situations: Locale formatting (phones, dates), admin tightened a pattern after go-live, widget input lacks client-side masking.

Related errors


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