From 31336c6f5594d7f621f47e862c0cd23a0a541bb7 Mon Sep 17 00:00:00 2001 From: Samuel Rowe Date: Tue, 26 Jul 2022 09:56:40 +0530 Subject: [PATCH] feat(frontend): implemented Yup port validation --- .../components/Modal/Service/form-utils.ts | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/services/frontend/src/components/Modal/Service/form-utils.ts b/services/frontend/src/components/Modal/Service/form-utils.ts index d8600c0..51f4be0 100644 --- a/services/frontend/src/components/Modal/Service/form-utils.ts +++ b/services/frontend/src/components/Modal/Service/form-utils.ts @@ -13,6 +13,23 @@ const initialValues: IEditServiceForm = { labels: [] }; +yup.addMethod(yup.string, "port", function (message) { + return this.test("test-port", message, function (value): + | boolean + | yup.ValidationError { + const { path, createError } = this; + + if (value) { + const result = parseInt(value, 10); + if (isNaN(result) || result < 0 || result > 65535) { + return createError({ path, message }); + } + } + + return true; + }); +}); + export const validationSchema = yup.object({ serviceName: yup .string() @@ -29,8 +46,12 @@ export const validationSchema = yup.object({ .required("Container name is required"), ports: yup.array( yup.object({ - hostPort: yup.string().required("Host port is required"), - containerPort: yup.string(), + hostPort: (yup.string().required("Host port is required") as any).port( + "Host port should be an integer in the range 0-65535" + ), + containerPort: (yup.string() as any).port( + "Container port should be an integer in the range 0-65535" + ), protocol: yup .string() .oneOf(["tcp", "udp"], "Protocol should be tcp or udp")