From 9b76bd566a8d4a6388e8c2f1f16aad01dd7f9c0b Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 9 Dec 2024 13:35:00 +0800 Subject: [PATCH] Update monitor and infrastrucutre validation to allow for port numbers --- Client/src/Validation/validation.js | 44 +++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/Client/src/Validation/validation.js b/Client/src/Validation/validation.js index 52ed3aa10..a7d6b3eb6 100644 --- a/Client/src/Validation/validation.js +++ b/Client/src/Validation/validation.js @@ -91,10 +91,24 @@ const credentials = joi.object({ }); const monitorValidation = joi.object({ - url: joi.string().uri({ allowRelative: true }).trim().messages({ - "string.empty": "This field is required.", - "string.uri": "The URL you provided is not valid.", - }), + url: joi + .string() + .trim() + .custom((value, helpers) => { + const urlRegex = + /^(https?:\/\/)?(([0-9]{1,3}\.){3}[0-9]{1,3}|[\da-z\.-]+)(\.[a-z\.]{2,6})?(:(\d+))?([\/\w \.-]*)*\/?$/i; + + if (!urlRegex.test(value)) { + return helpers.error("string.invalidUrl"); + } + + return value; + }) + .messages({ + "string.empty": "This field is required.", + "string.uri": "The URL you provided is not valid.", + "string.invalidUrl": "Please enter a valid URL with optional port", + }), name: joi.string().trim().max(50).allow("").messages({ "string.max": "This field should not exceed the 50 characters limit.", }), @@ -176,10 +190,24 @@ const advancedSettingsValidation = joi.object({ }); const infrastructureMonitorValidation = joi.object({ - url: joi.string().uri({ allowRelative: true }).trim().messages({ - "string.empty": "This field is required.", - "string.uri": "The URL you provided is not valid.", - }), + url: joi + .string() + .trim() + .custom((value, helpers) => { + const urlRegex = + /^(https?:\/\/)?(([0-9]{1,3}\.){3}[0-9]{1,3}|[\da-z\.-]+)(\.[a-z\.]{2,6})?(:(\d+))?([\/\w \.-]*)*\/?$/i; + + if (!urlRegex.test(value)) { + return helpers.error("string.invalidUrl"); + } + + return value; + }) + .messages({ + "string.empty": "This field is required.", + "string.uri": "The URL you provided is not valid.", + "string.invalidUrl": "Please enter a valid URL with optional port", + }), name: joi.string().trim().max(50).allow("").messages({ "string.max": "This field should not exceed the 50 characters limit.", }),