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 isFileValid (options: {
65 files: UploadFilesForCheck
67 maxSize: number | null
68 mimeTypeRegex: string | null
72 optional?: boolean // Default false
74 const { files, mimeTypeRegex, field, maxSize, optional = false } = options
77 if (!files) return optional
79 const fileArray = isArray(files)
83 if (!fileArray || !isArray(fileArray) || fileArray.length === 0) {
88 const file = fileArray[0]
89 if (!file || !file.originalname) return false
92 if ((maxSize !== null) && file.size > maxSize) return false
94 if (mimeTypeRegex === null) return true
96 return checkMimetypeRegex(file.mimetype, mimeTypeRegex)
99 function checkMimetypeRegex (fileMimeType: string, mimeTypeRegex: string) {
100 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(fileMimeType)
103 // ---------------------------------------------------------------------------
105 function toCompleteUUID (value: string) {
106 if (isShortUUID(value)) return shortToUUID(value)
111 function toCompleteUUIDs (values: string[]) {
112 return values.map(v => toCompleteUUID(v))
115 function toIntOrNull (value: string) {
116 const v = toValueOrNull(value)
118 if (v === null || v === undefined) return v
119 if (typeof v === 'number') return v
121 return validator.toInt('' + v)
124 function toBooleanOrNull (value: any) {
125 const v = toValueOrNull(value)
127 if (v === null || v === undefined) return v
128 if (typeof v === 'boolean') return v
130 return validator.toBoolean('' + v)
133 function toValueOrNull (value: string) {
134 if (value === 'null') return null
139 function toArray (value: any) {
140 if (value && isArray(value) === false) return [ value ]
145 function toIntArray (value: any) {
146 if (!value) return []
147 if (isArray(value) === false) return [ validator.toInt(value) ]
149 return value.map(v => validator.toInt(v))
152 // ---------------------------------------------------------------------------
162 isNotEmptyStringArray,