# All files / composer/lib/validation ValidateDraft.js

100% Statements 13/13
100% Branches 16/16
100% Functions 2/2
100% Lines 13/13

    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

    24x

    24x

    24x
    17x

    24x

    17x
    1x
    1x

    16x
    1x

    15x

    1x

    14x

     import { AttachmentTypes } from '../../AppConstants';
    import ValidationSuccess from './ValidationSuccess';
    import ValidationFail from './ValidationFail';

    function validateDraft(draft) {
      let validationResult = new ValidationSuccess();

      const hasVideoAttached =
            draft.enabledAttachmentType === AttachmentTypes.MEDIA && draft.video !== null;

      if (hasVideoAttached) {
        validationResult = validateVideoForService(draft.video, draft.service);
      }

      return validationResult;
    }

    function validateVideoForService(video, service) {
      // TODO: Refactor this method to be used only for reminders once back end is updated
      if (service.videoMaxSize && video.size > service.videoMaxSize) {
        const maxFileSizeInMb = service.videoMaxSize / 1024 / 1024;
        return new ValidationFail(`Please try again with a smaller file. Videos need to be smaller than ${maxFileSizeInMb}MB`);
      }

      if (service.videoMinDurationMs && video.durationMs < service.videoMinDurationMs) {
        return new ValidationFail(`Please lengthen your video and try again. Videos need to be longer than ${service.videoMinDurationMs / 1000} seconds`);
      }

      if (service.videoMaxDurationMs
          && video.durationMs > service.videoMaxDurationMs) {
        return new ValidationFail(`Please shorten your video and try again. Videos need to be under ${service.videoMaxDurationMs / 1000} seconds long`);
      }

      // if (service.videoMinAspectRatio && service.videoMaxAspectRatio) {
      //   const aspectRatio = video.width / video.height;
      //   if (aspectRatio < service.videoMinAspectRatio ||
      //     aspectRatio > service.videoMaxAspectRatio) {
      //     return new ValidationFail(`Sorry, your video needs to be between the aspect ratios ${service.videoMinAspectRatioHumanReadable} and ${service.videoMaxAspectRatioHumanReadable}. Please retry with another video`);
      //   }
      // }

      return new ValidationSuccess();
    }

    export default validateDraft;