diff --git a/govtool/frontend/src/components/organisms/CreateGovernanceActionSteps/StorageInformation.tsx b/govtool/frontend/src/components/organisms/CreateGovernanceActionSteps/StorageInformation.tsx index 3e878116f..d12b535cd 100644 --- a/govtool/frontend/src/components/organisms/CreateGovernanceActionSteps/StorageInformation.tsx +++ b/govtool/frontend/src/components/organisms/CreateGovernanceActionSteps/StorageInformation.tsx @@ -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>; @@ -131,6 +136,7 @@ export const StorageInformation = ({ setStep }: StorageInformationProps) => { value: URL_REGEX, message: t("createGovernanceAction.fields.validations.url"), }, + validate: isValidURLLength, }} /> } diff --git a/govtool/frontend/src/consts/dRepActions/fields.ts b/govtool/frontend/src/consts/dRepActions/fields.ts index 155ac6b87..90d9efec4 100644 --- a/govtool/frontend/src/consts/dRepActions/fields.ts +++ b/govtool/frontend/src/consts/dRepActions/fields.ts @@ -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: { @@ -47,5 +52,6 @@ export const Rules = { value: URL_REGEX, message: i18n.t("registration.fields.validations.url"), }, + validate: isValidURLLength, }, }; diff --git a/govtool/frontend/src/i18n/locales/en.ts b/govtool/frontend/src/i18n/locales/en.ts index 9570596a5..dd02c9b16 100644 --- a/govtool/frontend/src/i18n/locales/en.ts +++ b/govtool/frontend/src/i18n/locales/en.ts @@ -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: { diff --git a/govtool/frontend/src/utils/isValidFormat.ts b/govtool/frontend/src/utils/isValidFormat.ts index be9e65822..422c53bb6 100644 --- a/govtool/frontend/src/utils/isValidFormat.ts +++ b/govtool/frontend/src/utils/isValidFormat.ts @@ -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]+$/; @@ -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"); +}