2 import * as validator from 'validator'
4 function exists (value: any) {
5 return value !== undefined && value !== null
8 function isArray (value: any) {
9 return Array.isArray(value)
12 function isDateValid (value: string) {
13 return exists(value) && validator.isISO8601(value)
16 function isIdValid (value: string) {
17 return exists(value) && validator.isInt('' + value)
20 function isUUIDValid (value: string) {
21 return exists(value) && validator.isUUID('' + value, 4)
24 function isIdOrUUIDValid (value: string) {
25 return isIdValid(value) || isUUIDValid(value)
28 function isBooleanValid (value: any) {
29 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
32 function toIntOrNull (value: string) {
33 if (value === 'null') return null
35 return validator.toInt(value)
38 function toValueOrNull (value: string) {
39 if (value === 'null') return null
44 function toArray (value: string) {
45 if (value && isArray(value) === false) return [ value ]
50 function isFileValid (
51 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
52 mimeTypeRegex: string,
54 maxSize: number | null,
58 if (!files) return optional
59 if (isArray(files)) return optional
62 const fileArray = files[ field ]
63 if (!fileArray || fileArray.length === 0) {
67 // The file should exist
68 const file = fileArray[ 0 ]
69 if (!file || !file.originalname) return false
72 if ((maxSize !== null) && file.size > maxSize) return false
74 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
77 // ---------------------------------------------------------------------------