isValidChannelPattern static method

bool isValidChannelPattern(
  1. String pattern
)

Check if a string is a valid channel pattern (with wildcards)

Implementation

static bool isValidChannelPattern(String pattern) {
  // First check if it starts with / and contains only valid characters and wildcards
  if (!pattern.startsWith('/')) return false;

  // Split by / and check each segment
  final segments = pattern.split('/').where((s) => s.isNotEmpty).toList();

  bool hasWildcard = false;
  for (final segment in segments) {
    // Allow wildcards
    if (segment == '*' || segment == '**') {
      hasWildcard = true;
      continue;
    }

    // Check if segment contains only valid characters
    final validSegment =
        RegExp(r'^((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@))+$');
    if (!validSegment.hasMatch(segment)) return false;
  }

  // Must contain at least one wildcard to be a pattern
  return hasWildcard;
}