Skip to content

Input schemas defined with Zod does not use the inferred input type - #1189

Merged
KATT merged 9 commits into
mainfrom
katt/z.input
Nov 1, 2021
Merged

KATT merged 9 commits into
mainfrom
katt/z.input

Conversation

@KATT

@KATT KATT commented Oct 29, 2021 •

Copy link
Copy Markdown
Member

Closes #1182

Will make something like making a REST-adapter a lot easier as one could do param: z.union([z.string(), z.number()]).transform(v => Number(v)) to handle query params.

tl;dr Allows us to do this below:

trpc.router()
  .query('greeting', {
    input: z.object({
      hi: z.string().transform(s => s.length),
    }),
    resolve({ input }) {
      input.hi // => `number`, as expected
    },
  });

client.query('greeting', { hi: 'my greeting' }); 

@vercel

vercel Bot commented Oct 29, 2021 •

Copy link
Copy Markdown

@KATT KATT left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mmkal please help me review this one or feel free to fork it / push straight to it with simplifications

Comment on lines +36 to +37
export type ProcedureInputParserWithInputOutput<TInput, TParsedInput> =
ProcedureInputParserZodEsque<TInput, TParsedInput>;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit ugly -- needs to separate the ones where we can infer the input and output of the validator

Comment on lines +279 to +280
unknown extends TInput ? undefined : TInput,
unknown extends TParsedInput ? undefined : TParsedInput,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugly hack because this resolved as unknown on the ones without input

Comment on lines +283 to +299
public query<TPath extends string, TInput, TParsedInput, TOutput>(
path: TPath,
procedure: CreateProcedureWithInputOutputParser<
TContext,
TInput,
TParsedInput,
TOutput
>,
): Router<
TInputContext,
TContext,
TQueries &
Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
TMutations,
TSubscriptions,
TErrorShape
>;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish I didn't have to add these blocks

@KATT

KATT commented Nov 1, 2021

Copy link
Copy Markdown
Member Author

cc @mmkal - I merged this as it works, I'd love to see you having a stab at simplifying it if you feel inclined some day πŸ˜…

@kimroen

kimroen commented Nov 1, 2021

Copy link
Copy Markdown

Oh perfect! I was just trying to get this running locally so I could verify that it works and give some feedback here, but I couldn't figure out how to run it πŸ™ƒ I'll try this now!

@github-actions

github-actions Bot commented Oct 9, 2022

Copy link
Copy Markdown
Contributor

This pull request has been locked because it had no new activity for 30 days. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 9, 2022

This branch was successfully deployed

3 active deployments
Preview – next-prisma-starter β€” 064e3e94 Deployed Nov 1, 2021 by vercel[bot]
Preview – todomvc β€” 064e3e94 Deployed Nov 1, 2021 by vercel[bot]
Preview – www β€” 064e3e94 Deployed Nov 1, 2021 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

πŸ™‹β€β™‚οΈ help wanted Extra attention is needed πŸ™ review please needs community sign-off

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Input schemas defined with Zod does not use the inferred input type

2 participants