Aquila169 / zod-express-middleware

Express middleware to validate requests using zod schema's.
MIT License
86 stars 13 forks source link

TypedRequest<T> with a single parameter #3

Open Drarig29 opened 2 years ago

Drarig29 commented 2 years ago

I found it redundant to have to specify every parameter of TypedRequest everytime, so I created this file, which replaces TypedRequest<TParams, TQuery, TBody> with:

TypedRequest<S extends {
  params?: z.AnyZodObject,
  query?: z.AnyZodObject,
  body?: z.AnyZodObject,
}>

So now, I can do:

const mySchemaParams = z.object({});
const mySchemaQuery = z.object({});
const mySchemaBody = z.object({});

export const mySchema = {
  params: mySchemaParams,
  query: mySchemaQuery,
  body: mySchemaBody,
};

export type MyCustomRequest = TypedRequest<typeof mySchema>