misc.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import 'multer'
  2. import * as validator from 'validator'
  3. function exists (value: any) {
  4. return value !== undefined && value !== null
  5. }
  6. function isArray (value: any) {
  7. return Array.isArray(value)
  8. }
  9. function isNotEmptyIntArray (value: any) {
  10. return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0
  11. }
  12. function isArrayOf (value: any, validator: (value: any) => boolean) {
  13. return isArray(value) && value.every(v => validator(v))
  14. }
  15. function isDateValid (value: string) {
  16. return exists(value) && validator.isISO8601(value)
  17. }
  18. function isIdValid (value: string) {
  19. return exists(value) && validator.isInt('' + value)
  20. }
  21. function isUUIDValid (value: string) {
  22. return exists(value) && validator.isUUID('' + value, 4)
  23. }
  24. function isIdOrUUIDValid (value: string) {
  25. return isIdValid(value) || isUUIDValid(value)
  26. }
  27. function isBooleanValid (value: any) {
  28. return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
  29. }
  30. function toIntOrNull (value: string) {
  31. if (value === 'null') return null
  32. return validator.toInt(value)
  33. }
  34. function toValueOrNull (value: string) {
  35. if (value === 'null') return null
  36. return value
  37. }
  38. function toArray (value: any) {
  39. if (value && isArray(value) === false) return [ value ]
  40. return value
  41. }
  42. function toIntArray (value: any) {
  43. if (!value) return []
  44. if (isArray(value) === false) return [ validator.toInt(value) ]
  45. return value.map(v => validator.toInt(v))
  46. }
  47. function isFileValid (
  48. files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
  49. mimeTypeRegex: string,
  50. field: string,
  51. maxSize: number | null,
  52. optional = false
  53. ) {
  54. // Should have files
  55. if (!files) return optional
  56. if (isArray(files)) return optional
  57. // Should have a file
  58. const fileArray = files[ field ]
  59. if (!fileArray || fileArray.length === 0) {
  60. return optional
  61. }
  62. // The file should exist
  63. const file = fileArray[ 0 ]
  64. if (!file || !file.originalname) return false
  65. // Check size
  66. if ((maxSize !== null) && file.size > maxSize) return false
  67. return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
  68. }
  69. // ---------------------------------------------------------------------------
  70. export {
  71. exists,
  72. isArrayOf,
  73. isNotEmptyIntArray,
  74. isArray,
  75. isIdValid,
  76. isUUIDValid,
  77. isIdOrUUIDValid,
  78. isDateValid,
  79. toValueOrNull,
  80. isBooleanValid,
  81. toIntOrNull,
  82. toArray,
  83. toIntArray,
  84. isFileValid
  85. }