All files / packages/validation/src requestForms.ts

74.28% Statements 26/35
66.66% Branches 18/27
100% Functions 4/4
70.96% Lines 22/31

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                      1x 1x   4x               2x     2x 1x     1x 1x               1x     1x       1x 1x               2x 2x       2x                   1x       1x       1x   1x 1x       1x 1x              
import type {
  MealReservationFormType,
  ReallocationFormType,
  RoomBookingFormType,
  TimeOffFormType,
} from "@repo/types/form";
 
import {
  type FormValidator,
  validationFailure,
  validationSuccess,
} from "./types";
import { isInvalidTimeRange, isSameDate } from "./utils";
 
export const REQUEST_FORM_VALIDATION_MESSAGES = {
  END_TIME_AFTER_START: "End time must be after start time",
  END_DATE_AFTER_START: "End date must be same or after start date",
  DESTINATION_MUST_DIFFERENT_FROM_ORIGIN:
    "Destination must be different from current location",
  VEGETARIAN_REQUIRES_LUNCH: "Vegetarian option requires lunch",
} as const;
 
export const validateRoomBookingForm: FormValidator<RoomBookingFormType> = (
  data,
) => {
  if (!data.startTime || !data.endTime) {
    return validationSuccess<RoomBookingFormType>();
  }
 
  Eif (isInvalidTimeRange(data.startTime, data.endTime)) {
    return validationFailure<RoomBookingFormType>({
      endTime: REQUEST_FORM_VALIDATION_MESSAGES.END_TIME_AFTER_START,
    });
  }
 
  return validationSuccess<RoomBookingFormType>();
};
 
export const validateReallocationForm: FormValidator<ReallocationFormType> = (
  data,
) => {
  Iif (!data.dest || !data.origin) {
    return validationSuccess<ReallocationFormType>();
  }
 
  Eif (data.dest === data.origin) {
    return validationFailure<ReallocationFormType>({
      dest: REQUEST_FORM_VALIDATION_MESSAGES.DESTINATION_MUST_DIFFERENT_FROM_ORIGIN,
    });
  }
 
  return validationSuccess<ReallocationFormType>();
};
 
export const validateTimeOffForm: FormValidator<TimeOffFormType> = (data) => {
  Iif (data.startDate == null || data.endDate == null) {
    return validationSuccess<TimeOffFormType>();
  }
 
  switch (true) {
    case data.endDate.getTime() < data.startDate.getTime():
      return validationFailure<TimeOffFormType>({
        endDate: REQUEST_FORM_VALIDATION_MESSAGES.END_DATE_AFTER_START,
      });
    case !isSameDate(data.startDate, data.endDate):
      return validationSuccess<TimeOffFormType>();
    case !data.startTime || !data.endTime:
      return validationSuccess<TimeOffFormType>();
    case isInvalidTimeRange(data.startTime, data.endTime):
      return validationFailure<TimeOffFormType>({
        endTime: REQUEST_FORM_VALIDATION_MESSAGES.END_TIME_AFTER_START,
      });
    default:
      return validationSuccess<TimeOffFormType>();
  }
};
 
export const validateMealReservationForm: FormValidator<
  MealReservationFormType
> = (data) => {
  Iif (!data.vegetarian) {
    return validationSuccess<MealReservationFormType>();
  }
 
  Eif (!data.lunch) {
    return validationFailure<MealReservationFormType>({
      vegetarian: REQUEST_FORM_VALIDATION_MESSAGES.VEGETARIAN_REQUIRES_LUNCH,
    });
  }
 
  return validationSuccess<MealReservationFormType>();
};