summaryrefslogtreecommitdiff
path: root/src/util/validation.ts
diff options
context:
space:
mode:
authorRaphael Kabo <raphaelkabo@hey.com>2024-05-26 21:07:39 +0100
committerRaphael Kabo <raphaelkabo@hey.com>2024-05-26 21:07:39 +0100
commit82c6999ec2ebe572665cc7db4fdb8223cddfc24d (patch)
tree88936c7d5e438413c4f1cf8d3b62e19cd9576574 /src/util/validation.ts
parent43296cd88b9ab6f3ba1d5f4de5f76f44b68de82a (diff)
Properly validate URLs when editing
Diffstat (limited to 'src/util/validation.ts')
-rw-r--r--src/util/validation.ts30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/util/validation.ts b/src/util/validation.ts
index b9a0c8a..a3bea63 100644
--- a/src/util/validation.ts
+++ b/src/util/validation.ts
@@ -73,6 +73,20 @@ const validateEmail = (email: string) => {
return re.test(email);
};
+// From https://stackoverflow.com/a/43467144
+const validateUrl = (url: string) => {
+ if (!url) {
+ return false;
+ }
+ let validUrl;
+ try {
+ validUrl = new URL(url);
+ } catch (_) {
+ return false;
+ }
+ return validUrl.protocol === "http:" || validUrl.protocol === "https:";
+};
+
export const validateEventTime = (start: Date, end: Date): Error | boolean => {
if (moment(start).isAfter(moment(end))) {
return {
@@ -195,6 +209,14 @@ export const validateEventData = (
});
}
}
+ if (validatedData.eventURL) {
+ if (!validateUrl(validatedData.eventURL)) {
+ errors.push({
+ message: "Event link is invalid.",
+ field: "eventURL",
+ });
+ }
+ }
return {
data: validatedData,
@@ -226,6 +248,14 @@ export const validateGroupData = (
});
}
}
+ if (groupData.eventGroupURL) {
+ if (!validateUrl(groupData.eventGroupURL)) {
+ errors.push({
+ message: "Group link is invalid.",
+ field: "eventGroupURL",
+ });
+ }
+ }
const validatedData: ValidatedEventGroupData = {
...groupData,