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 areUUIDsValid (values: string[]) {
43 return isArray(values) && values.every(v => isUUIDValid(v))
46 function isIdOrUUIDValid (value: string) {
47 return isIdValid(value) || isUUIDValid(value)
50 function isBooleanValid (value: any) {
51 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
54 function isIntOrNull (value: any) {
55 return value === null || validator.isInt('' + value)
58 // ---------------------------------------------------------------------------
60 function isFileFieldValid (
61 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
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
81 function isFileMimeTypeValid (
82 files: UploadFilesForCheck,
83 mimeTypeRegex: string,
88 if (!files) return optional
89 if (isArray(files)) return optional
92 const fileArray = files[field]
93 if (!fileArray || fileArray.length === 0) {
97 // The file should exist
98 const file = fileArray[0]
99 if (!file || !file.originalname) return false
101 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
104 function isFileValid (
105 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
106 mimeTypeRegex: string,
108 maxSize: number | null,
112 if (!files) return optional
113 if (isArray(files)) return optional
115 // Should have a file
116 const fileArray = files[field]
117 if (!fileArray || fileArray.length === 0) {
121 // The file should exist
122 const file = fileArray[0]
123 if (!file || !file.originalname) return false
126 if ((maxSize !== null) && file.size > maxSize) return false
128 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
131 // ---------------------------------------------------------------------------
133 function toCompleteUUID (value: string) {
134 if (isShortUUID(value)) return shortToUUID(value)
139 function toCompleteUUIDs (values: string[]) {
140 return values.map(v => toCompleteUUID(v))
143 function toIntOrNull (value: string) {
144 const v = toValueOrNull(value)
146 if (v === null || v === undefined) return v
147 if (typeof v === 'number') return v
149 return validator.toInt('' + v)
152 function toBooleanOrNull (value: any) {
153 const v = toValueOrNull(value)
155 if (v === null || v === undefined) return v
156 if (typeof v === 'boolean') return v
158 return validator.toBoolean('' + v)
161 function toValueOrNull (value: string) {
162 if (value === 'null') return null
167 function toArray (value: any) {
168 if (value && isArray(value) === false) return [ value ]
173 function toIntArray (value: any) {
174 if (!value) return []
175 if (isArray(value) === false) return [ validator.toInt(value) ]
177 return value.map(v => validator.toInt(v))
180 // ---------------------------------------------------------------------------