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

96.55% Statements 28/29
92.11% Branches 35/38
100% Functions 3/3
96.55% Lines 28/29

    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

    17x

    17x

    17x

    17x
    13x
    4x
    3x

    17x

    13x
    1x
    1x

    12x
    1x

    11x

    1x

    10x
    10x
    10x

    2x

    8x

    3x
    3x
    3x

    1x

    1x

    2x
    2x

    2x

     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;

      const hasImageAttached =
            draft.enabledAttachmentType === AttachmentTypes.MEDIA && draft.images.length > 0;

      if (hasVideoAttached) {
        validationResult = validateVideoForService(draft.video, draft.service);
      } else if (hasImageAttached) {
        validationResult = validateImageForService(draft.images, draft.service);
      }
      return validationResult;
    }

    function validateVideoForService(video, service) {
      if (service.videoMaxSize && video.size > service.videoMaxSize) {
        const maxFileSizeInMb = service.videoMaxSize / 1024 / 1024;
        return new ValidationFail({
          message: `Due to Instagram limitations, we can't post videos directly to
          Instagram larger than ${maxFileSizeInMb}MB. You will receive a reminder to
          post manually when the time comes!`,
          composerId: 'instagram',
          code: null,
        });
      }

      if (service.videoMinDurationMs && video.durationMs < service.videoMinDurationMs) {
        return new ValidationFail({
          message: `Due to Instagram limitations, we can't post videos directly to
          Instagram shorter than ${service.videoMinDurationMs / 1000} seconds. You will
          receive a reminder to post manually when the time comes!`,
          composerId: 'instagram',
          code: null,
        });
      }

      if (service.videoMaxDurationMs
        && video.durationMs > service.videoMaxDurationMs) {
        return new ValidationFail({
          message: `Due to Instagram limitations, we can't post videos directly to
          Instagram longer than ${service.videoMaxDurationMs / 1000} seconds. You will
          receive a reminder to post manually when the time comes!`,
          composerId: 'instagram',
          code: null,
        });
      }

      Eif (service.videoMinAspectRatio && service.videoMaxAspectRatio) {
        const aspectRatio = video.width / video.height;
        if (aspectRatio < service.videoMinAspectRatio ||
          aspectRatio > service.videoMaxAspectRatio) {
          return new ValidationFail({
            message: `Due to Instagram limitations, we can't post videos directly to
            Instagram with aspect ratios outside the range ${service.videoMinAspectRatioHumanReadable}
            to ${service.videoMaxAspectRatioHumanReadable}. You will receive a reminder
            to post manually when the time comes!`,
            composerId: 'instagram',
            code: 'ASPECT_RATIO',
          });
        }
      }
      return new ValidationSuccess();
    }

    function validateImageForService(images, service) {
      Eif (service.imageMinAspectRatio && service.imageMaxAspectRatio) {
        const aspectRatio = images[0].width / images[0].height;
        if (aspectRatio < service.imageMinAspectRatio ||
          aspectRatio > service.imageMaxAspectRatio) {
          const message = `Due to Instagram limitations, we can't post images directly to
          Instagram with aspect ratios outside the range 4:5 to 1.91:1. You will receive
          a reminder to post manually when the time comes!`;
          return new ValidationFail({
            message,
            composerId: 'instagram',
            code: 'ASPECT_RATIO',
          });
        } else Eif (images.length > 1) {
          const message = `Due to Instagram limitations, we can't post galleries on your behalf.
          You will receive a reminder to post manually when the time comes!`;
          return new ValidationFail({
            message,
            composerId: 'instagram',
            code: 'GALLERY',
          });
        }
      }
      return new ValidationSuccess();
    }

    export default validateDraft;