diff --git a/packages/validate/CHANGELOG.md b/packages/validate/CHANGELOG.md index fb09e21b..9e413e66 100644 --- a/packages/validate/CHANGELOG.md +++ b/packages/validate/CHANGELOG.md @@ -1,5 +1,6 @@ # 3.0.0-alpha.1 * Add `MapField` and `UriField`. +* Fix how `FileField` handles allowed content types. # 3.0.0-alpha * Rewrite, based on `Field` and `Form`, rather than strings. diff --git a/packages/validate/lib/src/common_fields.dart b/packages/validate/lib/src/common_fields.dart index 8af80601..6885b6ff 100644 --- a/packages/validate/lib/src/common_fields.dart +++ b/packages/validate/lib/src/common_fields.dart @@ -261,6 +261,8 @@ class FileField extends Field { /// If provided, then the `content-type` must be present in this [Iterable]. final Iterable allowedContentTypes; + Iterable _allowedContentTypeStrings; + FileField(String name, {String label, bool isRequired = true, @@ -269,6 +271,8 @@ class FileField extends Field { this.allowedContentTypes}) : super(name, 'file', label: label, isRequired: isRequired) { assert(allowedContentTypes == null || allowedContentTypes.isNotEmpty); + _allowedContentTypeStrings = + _allowedContentTypeStrings?.map((c) => c.toString()); } @override @@ -288,12 +292,12 @@ class FileField extends Field { } else if (requireFilename && file.filename == null) { return FieldReadResult.failure( ['A filename must be given for file "$name".']); - } else if (allowedContentTypes != null && - !allowedContentTypes.contains(file.contentType)) { + } else if (_allowedContentTypeStrings != null && + !_allowedContentTypeStrings.contains(file.contentType.toString())) { return FieldReadResult.failure([ 'File "$name" cannot have content type ' '"${file.contentType}". Allowed types: ' - '${allowedContentTypes.join(', ')}' + '${_allowedContentTypeStrings.join(', ')}' ]); } else { return FieldReadResult.success(file);