allowedExtensions function

String? Function(List<BloomFile>) allowedExtensions(
  1. List<String> extensions, [
  2. String? message
])

Creates a validator for BloomFileField that fails when a file's extension is not in extensions.

Comparison is case-insensitive. Extensions may be specified with or without leading dots (e.g. ['.pdf', 'png']). Returns message (or 'File extension "$ext" is not allowed. Allowed: ...' by default).

final resumeField = BloomFileField(
  validators: [allowedExtensions(['.pdf', '.docx'])],
);

Implementation

String? Function(List<BloomFile>) allowedExtensions(List<String> extensions,
    [String? message]) {
  final lowerExts = extensions
      .map((e) => e.startsWith('.') ? e.toLowerCase() : '.$e'.toLowerCase())
      .toSet();
  return (files) {
    for (final file in files) {
      final ext = file.extension;
      if (ext.isEmpty || !lowerExts.contains(ext)) {
        return message ??
            'File extension "$ext" is not allowed. Allowed: ${extensions.join(', ')}.';
      }
    }
    return null;
  };
}