All files / validation imageValidation.ts

62.8% Statements 76/121
100% Branches 12/12
50% Functions 2/4
62.8% Lines 76/121

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 1221x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                     1x 1x 1x 11x 6x 6x 1x 1x 1x 1x 1x 1x 1x 6x 1x 1x 1x 1x 1x 1x 1x 4x 6x 1x 1x 1x 1x 1x 1x 1x 6x 1x 1x 1x 1x 1x 1x 1x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x                                                 1x 1x 1x                        
import { ValidationResult } from '@contracts/validationResult';
import { IImageParams } from '@helpers/imageHelper';
import { IMediaUpload, MediaUploadType } from '@web/contracts/mediaUpload';
import { shouldBeUrl } from './textValidation';
import { multiValidation } from './baseValidation';
import { IImageMetaDataProps } from '@contracts/image';
 
export interface IImageRestriction {
  maxWidth?: number;
  maxHeight?: number;
  minWidth?: number;
  minHeight?: number;
  maxSizeMb?: number;
}
 
export interface IImageWithParams extends File, IImageParams {}
 
export const isValidWebImage =
  (customErrMsg?: string) =>
  (value: IImageParams): ValidationResult => {
    if (value == null || value.name == null || value.fileSize == null) {
      return {
        isValid: false,
        errorMessage: customErrMsg ?? `Upload a valid image.`,
      };
    }

    return { isValid: true, errorMessage: '' };
  };
 
export const webImageRestrictions =
  (restrictions: IImageRestriction) =>
  (value: IImageParams): ValidationResult => {
    const imgName = value.name ?? 'unknown name';
    if (restrictions.maxHeight != null) {
      if (restrictions.maxHeight < value.height) {
        return {
          isValid: false,
          errorMessage: `Image '${imgName}' is too large, height should be less than or equal to ${restrictions.maxHeight}px.`,
        };
      }
    }
    if (restrictions.maxWidth != null) {
      if (restrictions.maxWidth < value.width) {
        return {
          isValid: false,
          errorMessage: `Image '${imgName}' is too large, width should be less than or equal to ${restrictions.maxWidth}px.`,
        };
      }
    }
 
    if (restrictions.minHeight != null) {
      if (restrictions.minHeight > value.height) {
        return {
          isValid: false,
          errorMessage: `Image '${imgName}' is too small, height should be greater than or equal to ${restrictions.minHeight}px.`,
        };
      }
    }
    if (restrictions.minWidth != null) {
      if (restrictions.minWidth > value.width) {
        return {
          isValid: false,
          errorMessage: `Image '${imgName}' is too small, width should be greater than or equal to ${restrictions.minWidth}px.`,
        };
      }
    }
    if (restrictions.maxSizeMb != null) {
      const sizeInMb = value.fileSize / (1024.0 * 1024.0);
      if (restrictions.maxSizeMb < sizeInMb) {
        return {
          isValid: false,
          errorMessage: `Image '${imgName}' is too large, image size limit is ${restrictions.maxSizeMb}mb.`,
        };
      }
    }
 
    return { isValid: true, errorMessage: '' };
  };
 
export const mediaUploadRestriction =
  (restrictions: IImageRestriction, customErrMsg?: string) =>
  (value: IMediaUpload): ValidationResult => {
    if (value == null || value.type == null) {
      return {
        isValid: false,
        errorMessage: customErrMsg ?? 'Invalid file uploaded',
      };
    }
    switch (value.type) {
      case MediaUploadType.File:
        const restrictFunc = multiValidation(
          isValidWebImage(customErrMsg),
          webImageRestrictions(restrictions),
        );
        if (value.file == null || value.imageDetails == null) {
          return {
            isValid: false,
            errorMessage: customErrMsg ?? 'Invalid file uploaded',
          };
        }
        return restrictFunc({ ...value.file, ...value.imageDetails });
      case MediaUploadType.ImageUrl:
      case MediaUploadType.VideoUrl:
        return shouldBeUrl(value.url);
    }
  };
 
export const apiFileUploadRestriction =
  (restrictions: IImageRestriction, name: string) =>
  (value: IImageMetaDataProps): ValidationResult => {
    const restrictionFunc = webImageRestrictions(restrictions);
    return restrictionFunc({
      width: value.width ?? 0,
      height: value.height ?? 0,
      fileExtension: '',
      fileSize: value.size ?? 0,
      type: '',
      name,
    });
  };