Skip to content

Commit 70e17de

Browse files
committed
fix(#3560): add precise types to UncontrolledImageInput component for improved type safety
1 parent c603e84 commit 70e17de

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

govtool/frontend/src/components/organisms/UncontrolledImageInput.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
21
import { useRef } from "react";
3-
import { useController } from "react-hook-form";
2+
import {
3+
Control,
4+
FieldPath,
5+
FieldValues,
6+
RegisterOptions,
7+
useController,
8+
} from "react-hook-form";
49
import { FormErrorMessage } from "../atoms";
510

6-
type UncontrolledImageInputProps = {
7-
name: string;
8-
control: any;
9-
rules?: any;
11+
type UncontrolledImageInputProps<
12+
TFieldValues extends FieldValues = FieldValues,
13+
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
14+
> = {
15+
name: TName;
16+
control: Control<TFieldValues>;
17+
rules?: RegisterOptions<TFieldValues, TName>;
1018
placeholder?: string;
1119
dataTestId?: string;
1220
};
1321

14-
export const UncontrolledImageInput = ({
22+
export const UncontrolledImageInput = <T extends FieldValues>({
1523
name,
1624
control,
1725
rules,
1826
placeholder,
1927
dataTestId,
20-
}: UncontrolledImageInputProps) => {
28+
}: UncontrolledImageInputProps<T>) => {
2129
const {
2230
field: { onChange },
2331
fieldState,

govtool/frontend/src/consts/dRepActions/fields.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ export const Rules = {
7777
value: IMAGE_REGEX,
7878
message: i18n.t("registration.fields.validations.image"),
7979
},
80-
validate: (value: string) => isValidImageUrl(value, { optional: true }),
80+
validate: (value: unknown) => isValidImageUrl(value, { optional: true }),
8181
},
8282
};

govtool/frontend/src/i18n/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,8 @@
389389
"tooLongUrl": "Url must be less than 128 bytes",
390390
"mustBeStakeAddress": "It must be reward address in bech32 format",
391391
"mustBeReceivingAddress": "Invalid payment address",
392-
"couldNotGenerateImageSha": "Could not generate image sha"
392+
"couldNotGenerateImageSha": "Could not generate image sha",
393+
"invalidValueType": "Invalid value type"
393394
}
394395
},
395396
"proposalDiscussion": {

govtool/frontend/src/utils/isValidFormat.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ export async function isDRepView(view?: string) {
8282
return i18n.t("forms.errors.mustBeDRepView");
8383
}
8484

85-
export async function isValidImageUrl(url: string, options?: Options) {
85+
export async function isValidImageUrl(url: unknown, options?: Options) {
86+
if (typeof url !== "string") {
87+
return i18n.t("forms.errors.invalidValueType");
88+
}
8689
if (options?.optional && !url) return true;
8790
if (!url.length) return false;
8891

0 commit comments

Comments
 (0)