emoss08 / Trenova

An Open Source AI-driven asset based transportation management system
http://trenova.app
Other
24 stars 5 forks source link

Convert Choice Fields to validate enums of the same type #241

Closed emoss08 closed 3 months ago

emoss08 commented 6 months ago

We should change the implementation of the string fields that take in a type, to ensure that yup is able to validate the choices of the field properly. For example

export type ServiceIncidentControlChoiceProps =
  | "Never"
  | "Pickup"
  | "Delivery"
  | "PickupAndDelivery"
  | "AllExceptShipper";

export enum ServiceIncidentControlEnum {
  Never = "Never",
  Pickup = "Pickup",
  Delivery = "Delivery",
  PickupAndDelivery = "PickupAndDelivery",
  AllExceptShipper = "AllExceptShipper",
}

export const serviceIncidentControlChoices = [
  { value: "Never", label: "Never" },
  { value: "Pickup", label: "Pickup" },
  { value: "Delivery", label: "Delivery" },
  { value: "PickupAndDelivery", label: "Pickup and Delivery" },
  { value: "AllExceptShipper", label: "All except shipper" },
] satisfies ReadonlyArray<IChoiceProps<ServiceIncidentControlChoiceProps>>;

This can be used in yup schemas as follows:

recordServiceIncident: mixed<ServiceIncidentControlEnum>()
      .required("Record Service Incident is required")
      .oneOf(Object.values(ServiceIncidentControlEnum)),

One thing to note is that the API will take care of the validation as well ,but it is good to have some form of client validation as well.