UploadedFileSize checks whether the validated [*filesystem.File] size is no more than the provided maxBytes. Example: validation.Field(&form.File, validation.By(validators.UploadedFileSize(1000)))
(maxBytes int64)
| 16 | // |
| 17 | // validation.Field(&form.File, validation.By(validators.UploadedFileSize(1000))) |
| 18 | func UploadedFileSize(maxBytes int64) validation.RuleFunc { |
| 19 | return func(value any) error { |
| 20 | v, ok := value.(*filesystem.File) |
| 21 | if !ok { |
| 22 | return ErrUnsupportedValueType |
| 23 | } |
| 24 | |
| 25 | if v == nil { |
| 26 | return nil // nothing to validate |
| 27 | } |
| 28 | |
| 29 | if v.Size > maxBytes { |
| 30 | return validation.NewError( |
| 31 | "validation_file_size_limit", |
| 32 | "Failed to upload {{.file}} - the maximum allowed file size is {{.maxSize}} bytes.", |
| 33 | ).SetParams(map[string]any{ |
| 34 | "file": cutStr(v.OriginalName, 300), |
| 35 | "maxSize": maxBytes, |
| 36 | }) |
| 37 | } |
| 38 | |
| 39 | return nil |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // UploadedFileMimeType checks whether the validated [*filesystem.File] |
| 44 | // mimetype is within the provided allowed mime types. |
searching dependent graphs…