validate: Fix FileField content type checks

This commit is contained in:
Tobe O 2020-05-04 17:03:32 -04:00
parent d233b1c37e
commit 8aad408c32
2 changed files with 8 additions and 3 deletions

View file

@ -1,5 +1,6 @@
# 3.0.0-alpha.1 # 3.0.0-alpha.1
* Add `MapField` and `UriField`. * Add `MapField` and `UriField`.
* Fix how `FileField` handles allowed content types.
# 3.0.0-alpha # 3.0.0-alpha
* Rewrite, based on `Field` and `Form`, rather than strings. * Rewrite, based on `Field` and `Form`, rather than strings.

View file

@ -261,6 +261,8 @@ class FileField extends Field<UploadedFile> {
/// If provided, then the `content-type` must be present in this [Iterable]. /// If provided, then the `content-type` must be present in this [Iterable].
final Iterable<MediaType> allowedContentTypes; final Iterable<MediaType> allowedContentTypes;
Iterable<String> _allowedContentTypeStrings;
FileField(String name, FileField(String name,
{String label, {String label,
bool isRequired = true, bool isRequired = true,
@ -269,6 +271,8 @@ class FileField extends Field<UploadedFile> {
this.allowedContentTypes}) this.allowedContentTypes})
: super(name, 'file', label: label, isRequired: isRequired) { : super(name, 'file', label: label, isRequired: isRequired) {
assert(allowedContentTypes == null || allowedContentTypes.isNotEmpty); assert(allowedContentTypes == null || allowedContentTypes.isNotEmpty);
_allowedContentTypeStrings =
_allowedContentTypeStrings?.map((c) => c.toString());
} }
@override @override
@ -288,12 +292,12 @@ class FileField extends Field<UploadedFile> {
} else if (requireFilename && file.filename == null) { } else if (requireFilename && file.filename == null) {
return FieldReadResult.failure( return FieldReadResult.failure(
['A filename must be given for file "$name".']); ['A filename must be given for file "$name".']);
} else if (allowedContentTypes != null && } else if (_allowedContentTypeStrings != null &&
!allowedContentTypes.contains(file.contentType)) { !_allowedContentTypeStrings.contains(file.contentType.toString())) {
return FieldReadResult.failure([ return FieldReadResult.failure([
'File "$name" cannot have content type ' 'File "$name" cannot have content type '
'"${file.contentType}". Allowed types: ' '"${file.contentType}". Allowed types: '
'${allowedContentTypes.join(', ')}' '${_allowedContentTypeStrings.join(', ')}'
]); ]);
} else { } else {
return FieldReadResult.success(file); return FieldReadResult.success(file);