trpc-openapi icon indicating copy to clipboard operation
trpc-openapi copied to clipboard

Feat/support number boolean date input params

Open jlalmes opened this issue 3 years ago • 0 comments

Seeking some feedback. This PR monkey patches the TRPCRouter to support number, boolean & Date out of the box (string was already supported) for queries inputs & pathParameters. I hope this would improve trpc-openapi incremental adoption, but I'm not sure wether I should just close this PR & force just encourage users to use z.preprocess instead...?

// Before PR
.query('some-query', {
  meta: { openapi: {...} },
  input: z.object({ 
    offset: z.preprocess((arg) => {
      if (typeof arg === 'string') return parseInt(arg);
      return arg;
    }, z.number().min(0)), 
    limit: z.preprocess((arg) => {
      if (typeof arg === 'string') return parseInt(arg);
      return arg;
    }, z.number().min(10).max(50)),
  }),
  ...
})
// After PR
.query('some-query', {
  meta: { openapi: {...} },
  input: z.object({ 
    offset: z.number().min(0), 
    limit: z.number().min(10).max(50),
  }),
  ...
})

jlalmes avatar Jul 07 '22 18:07 jlalmes