Skip to content

Commit

Permalink
add validation 128 bytes length for url
Browse files Browse the repository at this point in the history
  • Loading branch information
Sworzen1 committed Jun 11, 2024
1 parent 3d289c5 commit 092c9b6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import {
} from "@hooks";
import { Step } from "@molecules";
import { BgCard, ControlledField } from "@organisms";
import { URL_REGEX, ellipsizeText, openInNewTab } from "@utils";
import {
URL_REGEX,
ellipsizeText,
isValidURLLength,
openInNewTab,
} from "@utils";

type StorageInformationProps = {
setStep: Dispatch<SetStateAction<number>>;
Expand Down Expand Up @@ -131,6 +136,7 @@ export const StorageInformation = ({ setStep }: StorageInformationProps) => {
value: URL_REGEX,
message: t("createGovernanceAction.fields.validations.url"),
},
validate: isValidURLLength,
}}
/>
}
Expand Down
8 changes: 7 additions & 1 deletion govtool/frontend/src/consts/dRepActions/fields.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import i18n from "@/i18n";
import { EMAIL_REGEX, NICKNAME_REGEX, URL_REGEX } from "@/utils";
import {
EMAIL_REGEX,
NICKNAME_REGEX,
URL_REGEX,
isValidURLLength,
} from "@/utils";

export const Rules = {
BIO: {
Expand Down Expand Up @@ -47,5 +52,6 @@ export const Rules = {
value: URL_REGEX,
message: i18n.t("registration.fields.validations.url"),
},
validate: isValidURLLength,
},
};
1 change: 1 addition & 0 deletions govtool/frontend/src/i18n/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ export const en = {
hashInvalidFormat: "Invalid hash format",
hashInvalidLength: "Hash must be exactly 64 characters long",
urlTooLong: "Url must be less than 65 characters",
tooLongUrl: "Url must be less than 128 bytes",
urlInvalidFormat: "Invalid URL format",
},
registerAsDRep: {
Expand Down
13 changes: 13 additions & 0 deletions govtool/frontend/src/utils/isValidFormat.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import i18n from "@/i18n";

export const URL_REGEX =
/^(?:(?:https?:\/\/)?(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,})(?:\/[^\s]*)?)|(?:ipfs:\/\/[a-f0-9]+(?:\/[a-zA-Z0-9_]+)*)$|^$/;
export const HASH_REGEX = /^[0-9A-Fa-f]+$/;
Expand All @@ -13,3 +15,14 @@ export function isValidHashFormat(str: string) {
if (!str.length) return false;
return HASH_REGEX.test(str);
}

export function isValidURLLength(s: string) {
if (s.length > 128) {
return i18n.t("forms.errors.tooLongUrl");
}

const encoder = new TextEncoder();
const byteLength = encoder.encode(s).length;

return byteLength <= 128 ? true : i18n.t("forms.errors.tooLongUrl");
}

0 comments on commit 092c9b6

Please sign in to comment.