Skip to content

Commit

Permalink
fix(react-router): update standard schema (#2729)
Browse files Browse the repository at this point in the history
Co-authored-by: Manuel Schiller <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 11, 2024
1 parent e913b7c commit e13acec
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 44 deletions.
4 changes: 2 additions & 2 deletions examples/react/search-validator-adapters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"@tanstack/router-plugin": "^1.79.0",
"@tanstack/router-valibot-adapter": "^1.81.3",
"@tanstack/router-zod-adapter": "^1.81.3",
"arktype": "^2.0.0-rc.21",
"arktype": "2.0.0-rc.23",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"valibot": "1.0.0-beta.3",
"valibot": "1.0.0-beta.5",
"zod": "^3.23.8"
},
"devDependencies": {
Expand Down
23 changes: 13 additions & 10 deletions packages/react-router/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,13 @@ export interface SearchValidatorAdapter<TInput, TOutput> {

export type AnySearchValidatorAdapter = SearchValidatorAdapter<any, any>

export interface StandardSchemaValidatorProps<TInput, TOutput> {
readonly types?: StandardSchemaValidatorTypes<TInput, TOutput> | undefined
readonly validate: AnyStandardSchemaValidate
}

export interface StandardSchemaValidator<TInput, TOutput> {
readonly '~types'?: StandardSchemaValidatorTypes<TInput, TOutput> | undefined
readonly '~validate': AnyStandardSchemaValidate
readonly '~standard': StandardSchemaValidatorProps<TInput, TOutput>
}

export type AnyStandardSchemaValidator = StandardSchemaValidator<any, any>
Expand All @@ -529,6 +533,7 @@ export interface StandardSchemaValidatorTypes<TInput, TOutput> {

export interface AnyStandardSchemaValidateSuccess {
readonly value: any
readonly issues?: undefined
}

export interface AnyStandardSchemaValidateFailure {
Expand All @@ -539,13 +544,11 @@ export interface AnyStandardSchemaValidateIssue {
readonly message: string
}

export interface AnyStandardSchemaValidateInput {
readonly value: any
}

export type AnyStandardSchemaValidate = (
input: AnyStandardSchemaValidateInput,
) => AnyStandardSchemaValidateSuccess | AnyStandardSchemaValidateFailure
value: unknown,
) =>
| (AnyStandardSchemaValidateSuccess | AnyStandardSchemaValidateFailure)
| Promise<AnyStandardSchemaValidateSuccess | AnyStandardSchemaValidateFailure>

export type AnySearchValidatorFn = SearchValidatorFn<any, any>

Expand Down Expand Up @@ -681,7 +684,7 @@ export type ResolveSearchSchemaFnInput<TSearchValidator> =

export type ResolveSearchSchemaInput<TSearchValidator> =
TSearchValidator extends AnyStandardSchemaValidator
? NonNullable<TSearchValidator['~types']>['input']
? NonNullable<TSearchValidator['~standard']['types']>['input']
: TSearchValidator extends AnySearchValidatorAdapter
? TSearchValidator['types']['input']
: TSearchValidator extends AnySearchValidatorObj
Expand All @@ -698,7 +701,7 @@ export type ResolveSearchSchema<TSearchValidator> =
unknown extends TSearchValidator
? TSearchValidator
: TSearchValidator extends AnyStandardSchemaValidator
? NonNullable<TSearchValidator['~types']>['output']
? NonNullable<TSearchValidator['~standard']['types']>['output']
: TSearchValidator extends AnySearchValidatorAdapter
? TSearchValidator['types']['output']
: TSearchValidator extends AnySearchValidatorObj
Expand Down
7 changes: 5 additions & 2 deletions packages/react-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,14 @@ function validateSearch(
): unknown {
if (validateSearch == null) return {}

if ('~validate' in validateSearch) {
const result = validateSearch['~validate']({ value: input })
if ('~standard' in validateSearch) {
const result = validateSearch['~standard'].validate(input)

if ('value' in result) return result.value

if (result instanceof Promise)
throw new SearchParamError('Async validation not supported')

throw new SearchParamError(JSON.stringify(result.issues, undefined, 2))
}

Expand Down
4 changes: 2 additions & 2 deletions packages/router-valibot-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@
"@tanstack/react-router": "workspace:*",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.0.1",
"valibot": "1.0.0-beta.3"
"valibot": "1.0.0-beta.5"
},
"peerDependencies": {
"@tanstack/react-router": ">=1.43.2",
"valibot": "^1.0.0 || ^1.0.0-beta || ^1.0.0-rc"
"valibot": "^1.0.0 || ^1.0.0-beta.5 || ^1.0.0-rc"
}
}
76 changes: 48 additions & 28 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e13acec

Please sign in to comment.