Skip to content

Commit db7178a

Browse files
committed
fix: filter invalid query params without failing to match
1 parent 1e446ff commit db7178a

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

packages/router/src/experimental/route-resolver/matchers/matcher-pattern-query.spec.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,24 @@ describe('MatcherPatternQueryParam', () => {
220220
expect(matcher.build({ count: 42 })).toEqual({ c: '42' })
221221
})
222222

223-
it('throws on error without default', () => {
223+
it('swallows parser miss for optional param without default', () => {
224224
const matcher = new MatcherPatternQueryParam(
225225
'count',
226226
'c',
227227
'value',
228228
PARAM_PARSER_INT
229229
)
230-
expect(() => matcher.match({ c: 'invalid' })).toThrow(MatchMiss)
230+
// optional query params should not propagate parser misses
231+
expect(matcher.match({ c: 'invalid' })).toEqual({ count: undefined })
232+
})
233+
234+
it('swallows parser miss for optional array param without default', () => {
235+
const matcher = new MatcherPatternQueryParam('ids', 'id', 'array', {
236+
get: () => {
237+
throw new MatchMiss()
238+
},
239+
})
240+
expect(matcher.match({ id: ['1', '2'] })).toEqual({ ids: undefined })
231241
})
232242

233243
it('falls back to default on parser error', () => {

packages/router/src/experimental/route-resolver/matchers/matcher-pattern-query.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ export class MatcherPatternQueryParam<
6464
valueBeforeParse
6565
) as T
6666
} catch (error) {
67-
// if there is a miss but we have a default, use it
68-
// otherwise rethrow the error
69-
if (this.defaultValue === undefined) {
67+
// only propagate the miss for required params without a default
68+
// otherwise let the value fall back to the default (or undefined)
69+
if (this.required && this.defaultValue === undefined) {
7070
throw error
7171
}
7272
// ensure the default value is used
@@ -83,7 +83,9 @@ export class MatcherPatternQueryParam<
8383
valueBeforeParse
8484
) as T)
8585
} catch (error) {
86-
if (this.defaultValue === undefined) {
86+
// only propagate the miss for required params without a default
87+
// otherwise let the value fall back to the default (or undefined)
88+
if (this.required && this.defaultValue === undefined) {
8789
throw error
8890
}
8991
}

0 commit comments

Comments
 (0)