import 'multer'
-import validator from 'validator'
+import { UploadFilesForCheck } from 'express'
import { sep } from 'path'
+import validator from 'validator'
function exists (value: any) {
return value !== undefined && value !== null
return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
}
+function isIntOrNull (value: any) {
+ return value === null || validator.isInt('' + value)
+}
+
function toIntOrNull (value: string) {
const v = toValueOrNull(value)
return value.map(v => validator.toInt(v))
}
+function isFileFieldValid (
+ files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
+ field: string,
+ optional = false
+) {
+ // Should have files
+ if (!files) return optional
+ if (isArray(files)) return optional
+
+ // Should have a file
+ const fileArray = files[field]
+ if (!fileArray || fileArray.length === 0) {
+ return optional
+ }
+
+ // The file should exist
+ const file = fileArray[0]
+ if (!file || !file.originalname) return false
+ return file
+}
+
+function isFileMimeTypeValid (
+ files: UploadFilesForCheck,
+ mimeTypeRegex: string,
+ field: string,
+ optional = false
+) {
+ // Should have files
+ if (!files) return optional
+ if (isArray(files)) return optional
+
+ // Should have a file
+ const fileArray = files[field]
+ if (!fileArray || fileArray.length === 0) {
+ return optional
+ }
+
+ // The file should exist
+ const file = fileArray[0]
+ if (!file || !file.originalname) return false
+
+ return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
+}
+
function isFileValid (
files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
mimeTypeRegex: string,
if (isArray(files)) return optional
// Should have a file
- const fileArray = files[ field ]
+ const fileArray = files[field]
if (!fileArray || fileArray.length === 0) {
return optional
}
// The file should exist
- const file = fileArray[ 0 ]
+ const file = fileArray[0]
if (!file || !file.originalname) return false
// Check size
isArrayOf,
isNotEmptyIntArray,
isArray,
+ isIntOrNull,
isIdValid,
isSafePath,
isUUIDValid,
toIntOrNull,
toArray,
toIntArray,
+ isFileFieldValid,
+ isFileMimeTypeValid,
isFileValid
}