Skip to content

Commit 22cc026

Browse files
committed
Add type tests for additional validator formats
1 parent 38af395 commit 22cc026

File tree

2 files changed

+89
-6
lines changed

2 files changed

+89
-6
lines changed

packages/router-core/src/validators.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,9 @@ export type ResolveValidatorInput<TValidator> =
104104
? ResolveValidatorInputFn<TValidator['parse']>
105105
: ResolveValidatorInputFn<TValidator>
106106

107-
export type ResolveValidatorOutputFn<TValidator> = TValidator extends (
108-
...args: any
109-
) => infer TSchema
110-
? TSchema
111-
: AnySchema
107+
export type ResolveValidatorOutputFn<TValidator> = Awaited<
108+
TValidator extends (...args: any) => infer TSchema ? TSchema : AnySchema
109+
>
112110

113111
export type ResolveValidatorOutput<TValidator> = unknown extends TValidator
114112
? TValidator

packages/start-client-core/src/tests/createServerFn.test-d.ts

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ test('createServerFn without middleware', () => {
3131
})
3232
})
3333

34-
test('createServerFn with validator', () => {
34+
test('createServerFn with validator function', () => {
3535
const fnAfterValidator = createServerFn({
3636
method: 'GET',
3737
}).inputValidator((input: { input: string }) => ({
@@ -62,6 +62,91 @@ test('createServerFn with validator', () => {
6262
expectTypeOf<ReturnType<typeof fn>>().resolves.toEqualTypeOf<void>()
6363
})
6464

65+
test('createServerFn with async validator function', () => {
66+
const fnAfterValidator = createServerFn({
67+
method: 'GET',
68+
}).inputValidator((input: string) => Promise.resolve(input))
69+
70+
expectTypeOf(fnAfterValidator).toHaveProperty('handler')
71+
expectTypeOf(fnAfterValidator).toHaveProperty('middleware')
72+
expectTypeOf(fnAfterValidator).not.toHaveProperty('inputValidator')
73+
74+
const fn = fnAfterValidator.handler((options) => {
75+
expectTypeOf(options).toEqualTypeOf<{
76+
method: 'GET'
77+
context: undefined
78+
data: string
79+
signal: AbortSignal
80+
}>()
81+
})
82+
83+
expectTypeOf(fn).parameter(0).toEqualTypeOf<{
84+
data: string
85+
headers?: HeadersInit
86+
signal?: AbortSignal
87+
}>()
88+
89+
expectTypeOf<ReturnType<typeof fn>>().resolves.toEqualTypeOf<void>()
90+
})
91+
92+
test('createServerFn with validator with parse method', () => {
93+
const fnAfterValidator = createServerFn({
94+
method: 'GET',
95+
}).inputValidator({
96+
parse: (input: string) => input,
97+
})
98+
99+
expectTypeOf(fnAfterValidator).toHaveProperty('handler')
100+
expectTypeOf(fnAfterValidator).toHaveProperty('middleware')
101+
expectTypeOf(fnAfterValidator).not.toHaveProperty('inputValidator')
102+
103+
const fn = fnAfterValidator.handler((options) => {
104+
expectTypeOf(options).toEqualTypeOf<{
105+
method: 'GET'
106+
context: undefined
107+
data: string
108+
signal: AbortSignal
109+
}>()
110+
})
111+
112+
expectTypeOf(fn).parameter(0).toEqualTypeOf<{
113+
data: string
114+
headers?: HeadersInit
115+
signal?: AbortSignal
116+
}>()
117+
118+
expectTypeOf<ReturnType<typeof fn>>().resolves.toEqualTypeOf<void>()
119+
})
120+
121+
test('createServerFn with async validator with parse method', () => {
122+
const fnAfterValidator = createServerFn({
123+
method: 'GET',
124+
}).inputValidator({
125+
parse: (input: string) => Promise.resolve(input),
126+
})
127+
128+
expectTypeOf(fnAfterValidator).toHaveProperty('handler')
129+
expectTypeOf(fnAfterValidator).toHaveProperty('middleware')
130+
expectTypeOf(fnAfterValidator).not.toHaveProperty('inputValidator')
131+
132+
const fn = fnAfterValidator.handler((options) => {
133+
expectTypeOf(options).toEqualTypeOf<{
134+
method: 'GET'
135+
context: undefined
136+
data: string
137+
signal: AbortSignal
138+
}>()
139+
})
140+
141+
expectTypeOf(fn).parameter(0).toEqualTypeOf<{
142+
data: string
143+
headers?: HeadersInit
144+
signal?: AbortSignal
145+
}>()
146+
147+
expectTypeOf<ReturnType<typeof fn>>().resolves.toEqualTypeOf<void>()
148+
})
149+
65150
test('createServerFn with standard validator', () => {
66151
interface SyncValidator {
67152
readonly '~standard': {

0 commit comments

Comments
 (0)