2 import { UploadFilesForCheck } from 'express'
3 import { sep } from 'path'
4 import validator from 'validator'
5 import { isShortUUID, shortToUUID } from '@shared/extra-utils'
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 isNotEmptyStringArray (value: any) {
27 return Array.isArray(value) && value.every(v => typeof v === 'string' && v.length !== 0) && value.length !== 0
30 function isArrayOf (value: any, validator: (value: any) => boolean) {
31 return isArray(value) && value.every(v => validator(v))
34 function isDateValid (value: string) {
35 return exists(value) && validator.isISO8601(value)
38 function isIdValid (value: string) {
39 return exists(value) && validator.isInt('' + value)
42 function isUUIDValid (value: string) {
43 return exists(value) && validator.isUUID('' + value, 4)
46 function areUUIDsValid (values: string[]) {
47 return isArray(values) && values.every(v => isUUIDValid(v))
50 function isIdOrUUIDValid (value: string) {
51 return isIdValid(value) || isUUIDValid(value)
54 function isBooleanValid (value: any) {
55 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
58 function isIntOrNull (value: any) {
59 return value === null || validator.isInt('' + value)
62 // ---------------------------------------------------------------------------
64 function isFileFieldValid (
65 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
70 if (!files) return optional
71 if (isArray(files)) return optional
74 const fileArray = files[field]
75 if (!fileArray || fileArray.length === 0) {
79 // The file should exist
80 const file = fileArray[0]
81 if (!file || !file.originalname) return false
85 function isFileMimeTypeValid (
86 files: UploadFilesForCheck,
87 mimeTypeRegex: string,
92 if (!files) return optional
93 if (isArray(files)) return optional
96 const fileArray = files[field]
97 if (!fileArray || fileArray.length === 0) {
101 // The file should exist
102 const file = fileArray[0]
103 if (!file || !file.originalname) return false
105 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
108 function isFileValid (
109 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
110 mimeTypeRegex: string,
112 maxSize: number | null,
116 if (!files) return optional
117 if (isArray(files)) return optional
119 // Should have a file
120 const fileArray = files[field]
121 if (!fileArray || fileArray.length === 0) {
125 // The file should exist
126 const file = fileArray[0]
127 if (!file || !file.originalname) return false
130 if ((maxSize !== null) && file.size > maxSize) return false
132 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
135 // ---------------------------------------------------------------------------
137 function toCompleteUUID (value: string) {
138 if (isShortUUID(value)) return shortToUUID(value)
143 function toCompleteUUIDs (values: string[]) {
144 return values.map(v => toCompleteUUID(v))
147 function toIntOrNull (value: string) {
148 const v = toValueOrNull(value)
150 if (v === null || v === undefined) return v
151 if (typeof v === 'number') return v
153 return validator.toInt('' + v)
156 function toBooleanOrNull (value: any) {
157 const v = toValueOrNull(value)
159 if (v === null || v === undefined) return v
160 if (typeof v === 'boolean') return v
162 return validator.toBoolean('' + v)
165 function toValueOrNull (value: string) {
166 if (value === 'null') return null
171 function toArray (value: any) {
172 if (value && isArray(value) === false) return [ value ]
177 function toIntArray (value: any) {
178 if (!value) return []
179 if (isArray(value) === false) return [ validator.toInt(value) ]
181 return value.map(v => validator.toInt(v))
184 // ---------------------------------------------------------------------------
194 isNotEmptyStringArray,