isValidUserJid static method

bool isValidUserJid({
  1. required String userJid,
})

Validates a user JID (Jabber ID).

This method checks if the provided userJid is a valid JID. A valid JID must:

  • Contain exactly one '@' character.
  • Have a non-empty local part (the part before the '@').
  • Have a non-empty domain part (the part after the '@').

Parameters:

  • userJid: The JID to be validated. This parameter is required.

Returns:

  • true if the userJid is valid.
  • false if the userJid is invalid.

The method logs debug messages using LogMessage.d to indicate the reason for invalidity.

Example usage:

bool isValid = isValidUserJid(userJid: 'user@example.com');
if (isValid) {
  print('The JID is valid.');
} else {
  print('The JID is invalid.');
}

Implementation

static bool isValidUserJid({required String userJid}) {
  int atIndex = userJid.indexOf('@');
  if (atIndex == -1) {
    LogMessage.d("isValidUserJid",
        "Invalid JID: '$userJid' does not contain a '@' character.");
    return false;
  } else if (userJid.indexOf('@', atIndex + 1) != -1) {
    LogMessage.d("isValidUserJid",
        "Invalid JID: '$userJid' contains multiple '@' characters.");
    return false;
  }

  String localPart = userJid.split('@')[0];
  if (localPart.isEmpty) {
    LogMessage.d(
        "isValidUserJid", "Invalid JID: '$userJid' has an empty localPart.");
    return false;
  }

  String domainPart = userJid.split('@')[1];
  if (domainPart.isEmpty) {
    LogMessage.d(
        "isValidUserJid", "Invalid JID: '$userJid' has an empty domainPart.");
    return false;
  }
  return true;
}