Skip to content

Commit bbad5c7

Browse files
committed
fix: prefer Standard Schema for input/output type inference
Since it is the new and standard thing, we should use Standard Schema to extract input and output types whenever the schema implements it. Fixes #6887
1 parent 7c93a1f commit bbad5c7

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

packages/server/src/unstable-core-do-not-import/parser.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,22 @@ export type ParserWithInputOutput<TInput, TParsedInput> =
6262
export type Parser = ParserWithInputOutput<any, any> | ParserWithoutInput<any>;
6363

6464
export type inferParser<TParser extends Parser> =
65-
TParser extends ParserWithInputOutput<infer $TIn, infer $TOut>
65+
TParser extends ParserStandardSchemaEsque<infer $TIn, infer $TOut>
6666
? {
6767
in: $TIn;
6868
out: $TOut;
6969
}
70-
: TParser extends ParserWithoutInput<infer $InOut>
70+
: TParser extends ParserWithInputOutput<infer $TIn, infer $TOut>
7171
? {
72-
in: $InOut;
73-
out: $InOut;
72+
in: $TIn;
73+
out: $TOut;
7474
}
75-
: never;
75+
: TParser extends ParserWithoutInput<infer $InOut>
76+
? {
77+
in: $InOut;
78+
out: $InOut;
79+
}
80+
: never;
7681

7782
export type ParseFn<TType> = (value: unknown) => Promise<TType> | TType;
7883

packages/tests/server/validators.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,11 @@ test('zod4 branded types', () => {
935935
expectTypeOf(opts.input.accountId).toEqualTypeOf<Types['output']>();
936936
return opts.input;
937937
}),
938+
939+
top: t.procedure.input(AccountId).query((opts) => {
940+
expectTypeOf(opts.input).toEqualTypeOf<Types['output']>();
941+
return opts.input;
942+
}),
938943
});
939944

940945
type RouterInput = inferRouterInputs<typeof router>;

www/docs/server/validators.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sidebar_label: Input & Output Validators
55
slug: /server/validators
66
---
77

8-
tRPC procedures may define validation logic for their input and/or output, and validators are also used to infer the types of inputs and outputs. We have first class support for many popular validators, and you can [integrate validators](#contributing-your-own-validator-library) which we don't directly support.
8+
tRPC procedures may define validation logic for their input and/or output, and validators are also used to infer the types of inputs and outputs (using the [Standard Schema](https://standardschema.dev) interface if available, or custom interfaces for supported validators if not). We have first class support for many popular validators, and you can [integrate validators](#contributing-your-own-validator-library) which we don't directly support.
99

1010
## Input Validators
1111

@@ -169,7 +169,7 @@ export type AppRouter = typeof appRouter;
169169

170170
## Library integrations
171171

172-
tRPC works out of the box with a number of popular validation and parsing libraries, including any library conforming to [standard-schema](https://standardschema.dev). The below are some examples of usage with validators which we officially maintain support for.
172+
tRPC works out of the box with a number of popular validation and parsing libraries, including any library conforming to [Standard Schema](https://standardschema.dev). The below are some examples of usage with validators which we officially maintain support for.
173173

174174
### With [Zod](https://github.com/colinhacks/zod)
175175

@@ -495,4 +495,4 @@ export type AppRouter = typeof appRouter;
495495

496496
If you work on a validator library which supports tRPC usage, please feel free to open a PR for this page with equivalent usage to the other examples here, and a link to your docs.
497497

498-
Integration with tRPC in most cases is as simple as meeting one of several existing type interfaces. Conforming to [standard-schema](https://standardschema.dev) is recommended, but in some cases we may accept a PR to add a new supported interface. Feel free to open an issue for discussion. You can check the existing supported interfaces and functions for parsing/validation [in code](https://github.com/trpc/trpc/blob/main/packages/server/src/unstable-core-do-not-import/parser.ts).
498+
Integration with tRPC in most cases is as simple as meeting one of several existing type interfaces. Conforming to [Standard Schema](https://standardschema.dev) is recommended, but in some cases we may accept a PR to add a new supported interface. Feel free to open an issue for discussion. You can check the existing supported interfaces and functions for parsing/validation [in code](https://github.com/trpc/trpc/blob/main/packages/server/src/unstable-core-do-not-import/parser.ts).

0 commit comments

Comments
 (0)