Skip to content

Commit cdfccf2

Browse files
committed
Improve type guard
1 parent ec596ee commit cdfccf2

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

resources/js/wayfinder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const queryParams = (options?: {
6060
});
6161

6262
for (const subKey in query[key]) {
63-
if (query[key][subKey] !== undefined) {
63+
if (['string', 'number', 'boolean'].includes(typeof query[key][subKey])) {
6464
params.set(`${key}[${subKey}]`, getValue(query[key][subKey]));
6565
}
6666
}

tests/QueryParams.test.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,22 @@ it("can merge with the form method", () => {
169169
});
170170
});
171171

172-
it("handles nested objects with undefined keys", () => {
172+
it("ignores nested object values with unallowed types", () => {
173173
window.location.search = "?parent=og";
174174

175175
const query = (): {
176-
good: string;
176+
string: string;
177+
number: number;
178+
boolean: boolean;
177179
} => {
178180
const obj = {
179-
good: 'value',
180-
bad: undefined,
181+
string: 'string',
182+
number: 5,
183+
boolean: true,
184+
undefined: undefined,
185+
null: null,
186+
array: [],
187+
object: {},
181188
}
182189

183190
return obj
@@ -190,7 +197,7 @@ it("handles nested objects with undefined keys", () => {
190197
},
191198
}),
192199
).toEqual({
193-
action: "/posts?parent=og&_method=HEAD&parent%5Bgood%5D=value",
200+
action: "/posts?parent=og&_method=HEAD&parent%5Bstring%5D=string&parent%5Bnumber%5D=5&parent%5Bboolean%5D=1",
194201
method: "get",
195202
});
196203
});

0 commit comments

Comments
 (0)