2 import { UploadFilesForCheck } from 'express'
3 import { sep } from 'path'
4 import validator from 'validator'
5 import { isShortUUID, shortToUUID } from '../uuid'
7 function exists (value: any) {
8 return value !== undefined && value !== null
11 function isSafePath (p: string) {
13 (p + '').split(sep).every(part => {
14 return [ '..' ].includes(part) === false
18 function isArray (value: any): value is any[] {
19 return Array.isArray(value)
22 function isNotEmptyIntArray (value: any) {
23 return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0
26 function isArrayOf (value: any, validator: (value: any) => boolean) {
27 return isArray(value) && value.every(v => validator(v))
30 function isDateValid (value: string) {
31 return exists(value) && validator.isISO8601(value)
34 function isIdValid (value: string) {
35 return exists(value) && validator.isInt('' + value)
38 function isUUIDValid (value: string) {
39 return exists(value) && validator.isUUID('' + value, 4)
42 function isIdOrUUIDValid (value: string) {
43 return isIdValid(value) || isUUIDValid(value)
46 function isBooleanValid (value: any) {
47 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
50 function isIntOrNull (value: any) {
51 return value === null || validator.isInt('' + value)
54 // ---------------------------------------------------------------------------
56 function isFileFieldValid (
57 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
62 if (!files) return optional
63 if (isArray(files)) return optional
66 const fileArray = files[field]
67 if (!fileArray || fileArray.length === 0) {
71 // The file should exist
72 const file = fileArray[0]
73 if (!file || !file.originalname) return false
77 function isFileMimeTypeValid (
78 files: UploadFilesForCheck,
79 mimeTypeRegex: string,
84 if (!files) return optional
85 if (isArray(files)) return optional
88 const fileArray = files[field]
89 if (!fileArray || fileArray.length === 0) {
93 // The file should exist
94 const file = fileArray[0]
95 if (!file || !file.originalname) return false
97 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
100 function isFileValid (
101 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
102 mimeTypeRegex: string,
104 maxSize: number | null,
108 if (!files) return optional
109 if (isArray(files)) return optional
111 // Should have a file
112 const fileArray = files[field]
113 if (!fileArray || fileArray.length === 0) {
117 // The file should exist
118 const file = fileArray[0]
119 if (!file || !file.originalname) return false
122 if ((maxSize !== null) && file.size > maxSize) return false
124 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
127 // ---------------------------------------------------------------------------
129 function toCompleteUUID (value: string) {
130 if (isShortUUID(value)) return shortToUUID(value)
135 function toIntOrNull (value: string) {
136 const v = toValueOrNull(value)
138 if (v === null || v === undefined) return v
139 if (typeof v === 'number') return v
141 return validator.toInt('' + v)
144 function toBooleanOrNull (value: any) {
145 const v = toValueOrNull(value)
147 if (v === null || v === undefined) return v
148 if (typeof v === 'boolean') return v
150 return validator.toBoolean('' + v)
153 function toValueOrNull (value: string) {
154 if (value === 'null') return null
159 function toArray (value: any) {
160 if (value && isArray(value) === false) return [ value ]
165 function toIntArray (value: any) {
166 if (!value) return []
167 if (isArray(value) === false) return [ validator.toInt(value) ]
169 return value.map(v => validator.toInt(v))
172 // ---------------------------------------------------------------------------