Skip to content

Commit 62e40bf

Browse files
ErwanDecosterPierreJeanjacquot
authored andcommitted
refactor: improve raw value extraction and validation logic in useFilterParam hook
1 parent 24732c5 commit 62e40bf

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/hooks/useFilterParam.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@ export function useFilterParam(
1717
const search = useSearch({ strict: false });
1818
const navigate = useNavigate();
1919

20-
const rawValue = (search?.[paramName] as string | undefined) ?? defaultValue;
21-
const value = allowedValues.includes(rawValue) ? rawValue : defaultValue;
20+
const rawCandidate =
21+
search && Object.prototype.hasOwnProperty.call(search, paramName)
22+
? (search as Record<string, unknown>)[paramName]
23+
: undefined;
24+
25+
const value =
26+
typeof rawCandidate === 'string' && allowedValues.includes(rawCandidate)
27+
? rawCandidate
28+
: defaultValue;
2229

2330
const setValue = (newValue: string) => {
2431
if (!allowedValues.includes(newValue)) return; // ignore invalid values

0 commit comments

Comments
 (0)