@tanstack/react-router 1.81.3 → 1.81.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/react-router",
3
- "version": "1.81.3",
3
+ "version": "1.81.5",
4
4
  "description": "Modern and scalable routing for React applications",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
package/src/route.ts CHANGED
@@ -515,9 +515,13 @@ export interface SearchValidatorAdapter<TInput, TOutput> {
515
515
 
516
516
  export type AnySearchValidatorAdapter = SearchValidatorAdapter<any, any>
517
517
 
518
+ export interface StandardSchemaValidatorProps<TInput, TOutput> {
519
+ readonly types?: StandardSchemaValidatorTypes<TInput, TOutput> | undefined
520
+ readonly validate: AnyStandardSchemaValidate
521
+ }
522
+
518
523
  export interface StandardSchemaValidator<TInput, TOutput> {
519
- readonly '~types'?: StandardSchemaValidatorTypes<TInput, TOutput> | undefined
520
- readonly '~validate': AnyStandardSchemaValidate
524
+ readonly '~standard': StandardSchemaValidatorProps<TInput, TOutput>
521
525
  }
522
526
 
523
527
  export type AnyStandardSchemaValidator = StandardSchemaValidator<any, any>
@@ -529,6 +533,7 @@ export interface StandardSchemaValidatorTypes<TInput, TOutput> {
529
533
 
530
534
  export interface AnyStandardSchemaValidateSuccess {
531
535
  readonly value: any
536
+ readonly issues?: undefined
532
537
  }
533
538
 
534
539
  export interface AnyStandardSchemaValidateFailure {
@@ -539,13 +544,11 @@ export interface AnyStandardSchemaValidateIssue {
539
544
  readonly message: string
540
545
  }
541
546
 
542
- export interface AnyStandardSchemaValidateInput {
543
- readonly value: any
544
- }
545
-
546
547
  export type AnyStandardSchemaValidate = (
547
- input: AnyStandardSchemaValidateInput,
548
- ) => AnyStandardSchemaValidateSuccess | AnyStandardSchemaValidateFailure
548
+ value: unknown,
549
+ ) =>
550
+ | (AnyStandardSchemaValidateSuccess | AnyStandardSchemaValidateFailure)
551
+ | Promise<AnyStandardSchemaValidateSuccess | AnyStandardSchemaValidateFailure>
549
552
 
550
553
  export type AnySearchValidatorFn = SearchValidatorFn<any, any>
551
554
 
@@ -681,7 +684,7 @@ export type ResolveSearchSchemaFnInput<TSearchValidator> =
681
684
 
682
685
  export type ResolveSearchSchemaInput<TSearchValidator> =
683
686
  TSearchValidator extends AnyStandardSchemaValidator
684
- ? NonNullable<TSearchValidator['~types']>['input']
687
+ ? NonNullable<TSearchValidator['~standard']['types']>['input']
685
688
  : TSearchValidator extends AnySearchValidatorAdapter
686
689
  ? TSearchValidator['types']['input']
687
690
  : TSearchValidator extends AnySearchValidatorObj
@@ -698,7 +701,7 @@ export type ResolveSearchSchema<TSearchValidator> =
698
701
  unknown extends TSearchValidator
699
702
  ? TSearchValidator
700
703
  : TSearchValidator extends AnyStandardSchemaValidator
701
- ? NonNullable<TSearchValidator['~types']>['output']
704
+ ? NonNullable<TSearchValidator['~standard']['types']>['output']
702
705
  : TSearchValidator extends AnySearchValidatorAdapter
703
706
  ? TSearchValidator['types']['output']
704
707
  : TSearchValidator extends AnySearchValidatorObj
package/src/router.ts CHANGED
@@ -585,11 +585,14 @@ function validateSearch(
585
585
  ): unknown {
586
586
  if (validateSearch == null) return {}
587
587
 
588
- if ('~validate' in validateSearch) {
589
- const result = validateSearch['~validate']({ value: input })
588
+ if ('~standard' in validateSearch) {
589
+ const result = validateSearch['~standard'].validate(input)
590
590
 
591
591
  if ('value' in result) return result.value
592
592
 
593
+ if (result instanceof Promise)
594
+ throw new SearchParamError('Async validation not supported')
595
+
593
596
  throw new SearchParamError(JSON.stringify(result.issues, undefined, 2))
594
597
  }
595
598
 
@@ -1511,8 +1514,8 @@ export class Router<
1511
1514
  })
1512
1515
 
1513
1516
  let search = fromSearch
1514
- if (opts._includeValidateSearch) {
1515
- let validatedSearch = this.options.search?.strict ? {} : search
1517
+ if (opts._includeValidateSearch && this.options.search?.strict) {
1518
+ let validatedSearch = {}
1516
1519
  matchedRoutesResult?.matchedRoutes.forEach((route) => {
1517
1520
  try {
1518
1521
  if (route.options.validateSearch) {
@@ -1535,10 +1538,10 @@ export class Router<
1535
1538
  const allMiddlewares =
1536
1539
  matchedRoutesResult?.matchedRoutes.reduce(
1537
1540
  (acc, route) => {
1538
- let middlewares: Array<SearchMiddleware<any>> = []
1541
+ const middlewares: Array<SearchMiddleware<any>> = []
1539
1542
  if ('search' in route.options) {
1540
1543
  if (route.options.search?.middlewares) {
1541
- middlewares = route.options.search.middlewares
1544
+ middlewares.push(...route.options.search.middlewares)
1542
1545
  }
1543
1546
  }
1544
1547
  // TODO remove preSearchFilters and postSearchFilters in v2
@@ -1572,7 +1575,25 @@ export class Router<
1572
1575
  }
1573
1576
  return result
1574
1577
  }
1575
- middlewares = [legacyMiddleware]
1578
+ middlewares.push(legacyMiddleware)
1579
+ }
1580
+ if (opts._includeValidateSearch && route.options.validateSearch) {
1581
+ const validate: SearchMiddleware<any> = ({ search, next }) => {
1582
+ try {
1583
+ const result = next(search)
1584
+ const validatedSearch = {
1585
+ ...result,
1586
+ ...(validateSearch(
1587
+ route.options.validateSearch,
1588
+ result,
1589
+ ) ?? {}),
1590
+ }
1591
+ return validatedSearch
1592
+ } catch (e) {
1593
+ // ignore errors here because they are already handled in matchRoutes
1594
+ }
1595
+ }
1596
+ middlewares.push(validate)
1576
1597
  }
1577
1598
  return acc.concat(middlewares)
1578
1599
  },