Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
emoss08 opened this issue Mar 15, 2024 · 0 comments
Closed

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

emoss08 opened this issue Mar 15, 2024 · 0 comments
Labels
client Change to client application enhancement New feature or request

Comments

@emoss08
Copy link
Owner

emoss08 commented Mar 15, 2024

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.

@emoss08 emoss08 added enhancement New feature or request client Change to client application labels Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client Change to client application enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants