Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Allow coercion when given union of coercible values #427

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 133 additions & 4 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"superjson": "^1.12.3",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
"typescript": "^5.0.4",
"zod": "^3.22.4"
}
}
10 changes: 9 additions & 1 deletion src/adapters/node-http/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { normalizePath } from '../../utils/path';
import { getInputOutputParsers } from '../../utils/procedure';
import {
instanceofZodTypeCoercible,
instanceofZodTypeKind,
instanceofZodTypeLikeVoid,
instanceofZodTypeObject,
unwrapZodType,
Expand Down Expand Up @@ -118,7 +119,14 @@ export const createOpenApiNodeHttpHandler = <
if (instanceofZodTypeObject(unwrappedSchema)) {
Object.values(unwrappedSchema.shape).forEach((shapeSchema) => {
const unwrappedShapeSchema = unwrapZodType(shapeSchema, false);
if (instanceofZodTypeCoercible(unwrappedShapeSchema)) {

if (instanceofZodTypeKind(unwrappedShapeSchema, z.ZodFirstPartyTypeKind.ZodUnion)) {
unwrappedShapeSchema.options.forEach((option) => {
if (instanceofZodTypeCoercible(option)) {
option._def.coerce = true;
}
});
} else if (instanceofZodTypeCoercible(unwrappedShapeSchema)) {
unwrappedShapeSchema._def.coerce = true;
}
});
Expand Down
6 changes: 5 additions & 1 deletion src/generator/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ export const getOpenApiPathsObject = (
) || []),
],
}),
responses: getResponsesObject(outputParser, openapi.example?.response, openapi.responseHeaders),
responses: getResponsesObject(
outputParser,
openapi.example?.response,
openapi.responseHeaders,
),
...(openapi.deprecated ? { deprecated: openapi.deprecated } : {}),
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/generator/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export const errorResponseObject: OpenAPIV3.ResponseObject = {
export const getResponsesObject = (
schema: unknown,
example: Record<string, any> | undefined,
headers: Record<string, OpenAPIV3.HeaderObject | OpenAPIV3.ReferenceObject> | undefined
headers: Record<string, OpenAPIV3.HeaderObject | OpenAPIV3.ReferenceObject> | undefined,
): OpenAPIV3.ResponsesObject => {
if (!instanceofZodType(schema)) {
throw new TRPCError({
Expand Down
9 changes: 8 additions & 1 deletion src/utils/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const instanceofZodType = (type: any): type is z.ZodTypeAny => {
export const instanceofZodTypeKind = <Z extends z.ZodFirstPartyTypeKind>(
type: z.ZodTypeAny,
zodTypeKind: Z,
): type is InstanceType<typeof z[Z]> => {
): type is InstanceType<(typeof z)[Z]> => {
return type?._def?.typeName === zodTypeKind;
};

Expand Down Expand Up @@ -106,6 +106,13 @@ export type ZodTypeCoercible = z.ZodNumber | z.ZodBoolean | z.ZodBigInt | z.ZodD

export const instanceofZodTypeCoercible = (_type: z.ZodTypeAny): _type is ZodTypeCoercible => {
const type = unwrapZodType(_type, false);

if (instanceofZodTypeKind(type, z.ZodFirstPartyTypeKind.ZodUnion)) {
return type._def.options.every(
(option) => instanceofZodTypeLikeString(option) || instanceofZodTypeCoercible(option),
);
}

return (
instanceofZodTypeKind(type, z.ZodFirstPartyTypeKind.ZodNumber) ||
instanceofZodTypeKind(type, z.ZodFirstPartyTypeKind.ZodBoolean) ||
Expand Down
2 changes: 2 additions & 0 deletions test/adapters/standalone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,7 @@ describe('standalone adapter', () => {
// @ts-expect-error - hack to disable zodSupportsCoerce
// eslint-disable-next-line import/namespace
zodUtils.zodSupportsCoerce = false;

{
const appRouter = t.router({
plusOne: t.procedure
Expand Down Expand Up @@ -1225,6 +1226,7 @@ describe('standalone adapter', () => {

close();
}

// @ts-expect-error - hack to re-enable zodSupportsCoerce
// eslint-disable-next-line import/namespace
zodUtils.zodSupportsCoerce = true;
Expand Down
Loading