validateComponentFile function

ValidationResult validateComponentFile(
  1. String filePath,
  2. String content,
  3. PluginFileType fileType
)

Validate the YAML frontmatter in a plugin component markdown file.

Implementation

ValidationResult validateComponentFile(
  String filePath,
  String content,
  PluginFileType fileType,
) {
  final errors = <ValidationError>[];
  final warnings = <ValidationWarning>[];

  final match = _frontmatterRegex.firstMatch(content);
  if (match == null) {
    warnings.add(
      ValidationWarning(
        path: 'frontmatter',
        message:
            'No frontmatter block found. Add YAML frontmatter between --- delimiters '
            'at the top of the file to set description and other metadata.',
      ),
    );
    return ValidationResult(
      success: true,
      errors: errors,
      warnings: warnings,
      filePath: filePath,
      fileType: fileType,
    );
  }

  // Note: Full YAML parsing would require a yaml package dependency.
  // Here we do basic key-value extraction for validation purposes.
  final frontmatterText = match.group(1) ?? '';
  final lines = frontmatterText.split('\n');
  final fields = <String, String>{};
  for (final line in lines) {
    final colonIdx = line.indexOf(':');
    if (colonIdx > 0) {
      final key = line.substring(0, colonIdx).trim();
      final value = line.substring(colonIdx + 1).trim();
      fields[key] = value;
    }
  }

  // Check description
  if (!fields.containsKey('description')) {
    final typeStr = fileType.name;
    warnings.add(
      ValidationWarning(
        path: 'description',
        message:
            'No description in frontmatter. A description helps users and Neomage '
            'understand when to use this $typeStr.',
      ),
    );
  }

  // Check shell field
  if (fields.containsKey('shell')) {
    final sh = fields['shell']!.trim().toLowerCase();
    if (sh != 'bash' && sh != 'powershell') {
      errors.add(
        ValidationError(
          path: 'shell',
          message:
              "shell must be 'bash' or 'powershell', got '${fields['shell']}'.",
        ),
      );
    }
  }

  return ValidationResult(
    success: errors.isEmpty,
    errors: errors,
    warnings: warnings,
    filePath: filePath,
    fileType: fileType,
  );
}