All files / packages/validation/src fieldRules.ts

92.1% Statements 35/38
66.66% Branches 8/12
100% Functions 10/10
90% Lines 27/30

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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 1411x                                                                   1x 4x       4x   2x       2x           2x   26x   4x     1x     1x       1x     1x       1x     1x       1x     1x   4x     1x       24x   6x         1x       1x       1x                         1x       1x   3x                              
import { ERROR_MESSAGES } from "@repo/constants/messages";
 
import type {
  MaintenanceFormType,
  MealReservationFormType,
  ReallocationFormType,
  RoomBookingFormType,
  TimeOffFormType,
} from "@repo/types/form";
 
interface LengthRule {
  value: number;
  message: string;
}
 
interface PatternRule {
  value: RegExp;
  message: string;
}
 
type ValidateRule = (value: unknown) => boolean | string;
 
export type FieldRule = {
  required?: string;
  minLength?: LengthRule;
  maxLength?: LengthRule;
  pattern?: PatternRule;
  validate?: ValidateRule;
} & Record<string, unknown>;
 
export type FormFieldRules<T extends object> = Partial<
  Record<Extract<keyof T, string>, FieldRule>
>;
 
const isRequiredValuePresent = (value: unknown): boolean => {
  Iif (value == null) {
    return false;
  }
 
  switch (typeof value) {
    case "string":
      return value.trim().length > 0;
    case "boolean":
      return value;
    case "object":
      return Array.isArray(value) ? value.length > 0 : true;
    default:
      return true;
  }
};
 
export const createRequiredRule = (
  message: string = ERROR_MESSAGES.FIELD_REQUIRED,
): FieldRule => ({
  required: message,
  validate: (value: unknown) => isRequiredValuePresent(value) || message,
});
 
export const createMinLengthRule = (
  value: number,
  message: string,
): FieldRule => ({
  minLength: { value, message },
});
 
export const createMaxLengthRule = (
  value: number,
  message: string,
): FieldRule => ({
  maxLength: { value, message },
});
 
export const createPatternRule = (
  value: RegExp,
  message: string,
): FieldRule => ({
  pattern: { value, message },
});
 
export const composeFieldRules = (
  ...rules: ReadonlyArray<FieldRule | undefined>
): FieldRule =>
  Object.assign(
    {},
    ...rules.filter((rule): rule is FieldRule => rule !== undefined),
  );
 
export const createRequiredFieldRules = <T extends object>(
  fields: ReadonlyArray<Extract<keyof T, string>>,
  message: string = ERROR_MESSAGES.FIELD_REQUIRED,
): FormFieldRules<T> => {
  const entries = fields.map((field) => [field, createRequiredRule(message)]);
 
  return Object.fromEntries(entries) as FormFieldRules<T>;
};
 
const REALLOCATION_REQUIRED_FIELDS: ReadonlyArray<
  Extract<keyof ReallocationFormType, string>
> = ["emails", "dest", "date", "time", "note"];
 
const ROOM_BOOKING_REQUIRED_FIELDS: ReadonlyArray<
  Extract<keyof RoomBookingFormType, string>
> = ["room", "date", "startTime", "endTime", "note"];
 
const TIME_OFF_REQUIRED_FIELDS: ReadonlyArray<
  Extract<keyof TimeOffFormType, string>
> = [
  "user",
  "to",
  "cc",
  "startDate",
  "startTime",
  "endDate",
  "endTime",
  "note",
];
 
const MEAL_RESERVATION_REQUIRED_FIELDS: ReadonlyArray<
  Extract<keyof MealReservationFormType, string>
> = ["user", "office"];
 
const MAINTENANCE_REQUIRED_FIELDS: ReadonlyArray<
  Extract<keyof MaintenanceFormType, string>
> = ["scope", "note"];
 
export const REQUEST_FORM_FIELD_RULES = {
  reallocation: createRequiredFieldRules<ReallocationFormType>(
    REALLOCATION_REQUIRED_FIELDS,
  ),
  roomBooking: createRequiredFieldRules<RoomBookingFormType>(
    ROOM_BOOKING_REQUIRED_FIELDS,
  ),
  timeOff: createRequiredFieldRules<TimeOffFormType>(TIME_OFF_REQUIRED_FIELDS),
  mealReservation: createRequiredFieldRules<MealReservationFormType>(
    MEAL_RESERVATION_REQUIRED_FIELDS,
  ),
  maintenance: createRequiredFieldRules<MaintenanceFormType>(
    MAINTENANCE_REQUIRED_FIELDS,
  ),
} as const;