validateOrganization function

ValidationResult validateOrganization(
  1. String org
)

Implementation

ValidationResult validateOrganization(String org) {
  if (org.isEmpty) {
    return const ValidationFailure(
      ruleName: 'not_empty',
      message: 'Organization identifier must not be empty.',
    );
  }

  final segments = org.split('.');
  if (segments.length < 2) {
    return const ValidationFailure(
      ruleName: 'at_least_two_segments',
      message:
          'Organization identifier must contain at least two segments (separated by a dot).',
    );
  }

  for (final segment in segments) {
    if (segment.isEmpty) {
      return const ValidationFailure(
        ruleName: 'segment_not_empty',
        message:
            'Each segment in the organization identifier must not be empty.',
      );
    }

    final validChars = RegExp(r'^[A-Za-z0-9_]+$');
    if (!validChars.hasMatch(segment)) {
      return const ValidationFailure(
        ruleName: 'segment_valid_characters',
        message:
            'Each segment in the organization identifier must contain only letters, digits, and underscores.',
      );
    }

    if (RegExp(r'^[0-9_]').hasMatch(segment)) {
      return const ValidationFailure(
        ruleName: 'segment_start_letter',
        message:
            'Each segment in the organization identifier must start with a letter.',
      );
    }
  }

  return const ValidationSuccess();
}