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 isNotEmptyIntArray (value: any) {
13 return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0
16 function isArrayOf (value: any, validator: (value: any) => boolean) {
17 return isArray(value) && value.every(v => validator(v))
20 function isDateValid (value: string) {
21 return exists(value) && validator.isISO8601(value)
24 function isIdValid (value: string) {
25 return exists(value) && validator.isInt('' + value)
28 function isUUIDValid (value: string) {
29 return exists(value) && validator.isUUID('' + value, 4)
32 function isIdOrUUIDValid (value: string) {
33 return isIdValid(value) || isUUIDValid(value)
36 function isBooleanValid (value: any) {
37 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
40 function toIntOrNull (value: string) {
41 if (value === 'null') return null
43 return validator.toInt(value)
46 function toValueOrNull (value: string) {
47 if (value === 'null') return null
52 function toArray (value: string) {
53 if (value && isArray(value) === false) return [ value ]
58 function isFileValid (
59 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
60 mimeTypeRegex: string,
62 maxSize: number | null,
66 if (!files) return optional
67 if (isArray(files)) return optional
70 const fileArray = files[ field ]
71 if (!fileArray || fileArray.length === 0) {
75 // The file should exist
76 const file = fileArray[ 0 ]
77 if (!file || !file.originalname) return false
80 if ((maxSize !== null) && file.size > maxSize) return false
82 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
85 // ---------------------------------------------------------------------------