validateContent method

ContentValidationResult validateContent(
  1. String content
)

Validates a single content string

Implementation

ContentValidationResult validateContent(String content) {
  if (content.isEmpty) {
    return ContentValidationResult.invalid(
      content,
      errorMessage: 'Content is empty',
    );
  }

  if (content.length < minContentLength) {
    return ContentValidationResult.invalid(
      content,
      errorMessage:
          'Content is too short (${content.length} < $minContentLength)',
    );
  }

  if (content.length > maxContentLength) {
    return ContentValidationResult.invalid(
      content,
      errorMessage:
          'Content is too long (${content.length} > $maxContentLength)',
    );
  }

  // Clean the content if needed
  String? cleanedContent;
  bool needsCleaning = false;

  if (cleanHtmlTags && _containsHtmlTags(content)) {
    cleanedContent = _cleanHtmlTags(content);
    needsCleaning = true;
  }

  if (normalizeWhitespace && _containsExcessiveWhitespace(content)) {
    cleanedContent = _normalizeWhitespace(cleanedContent ?? content);
    needsCleaning = true;
  }

  if (trimContent && (content.startsWith(' ') || content.endsWith(' '))) {
    cleanedContent = (cleanedContent ?? content).trim();
    needsCleaning = true;
  }

  if (needsCleaning) {
    return ContentValidationResult.cleaned(content, cleanedContent!);
  }

  return ContentValidationResult.valid(content);
}