Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS generics support for checkSchema function #829

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

gugu
Copy link

@gugu gugu commented Feb 7, 2020

No description provided.

@coveralls
Copy link

coveralls commented Feb 7, 2020

Coverage Status

Coverage remained the same at 100.0% when pulling 87c9e49 on Short-io:master into 4bbe421 on express-validator:master.

Copy link
Member

@fedeci fedeci left a comment

Choose a reason for hiding this comment

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

We can't default to any when no type is specified because ValidationChain would not override it.

// this is the result we get
(req: any, res: any, next: any): void

The only solution is the one suggested, but I'm not 100% satisfied with that.
/cc @gustavohenke

@@ -9,11 +9,11 @@ import { InternalRequest, Location } from '../base';
import { bindAll } from '../utils';
import { ContextBuilder } from '../context-builder';

export function check(
export function check<Req = any, Res = any, NextFn = any>(
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
export function check<Req = any, Res = any, NextFn = any>(
export function check<Req = Request, Res = any, NextFn = (error?: any => void)>(

Copy link
Member

Choose a reason for hiding this comment

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

I would even go one step further and make it Req extends Request instead

@@ -57,10 +57,17 @@ export type ValidationSchema = Schema;
const validLocations: Location[] = ['body', 'cookies', 'headers', 'params', 'query'];
const protectedNames = ['errorMessage', 'in'];

export function checkSchema(schema: Schema, defaultLocations: Location[] = validLocations) {
export function checkSchema<Req = any, Res = any, NextFn = any>(
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
export function checkSchema<Req = any, Res = any, NextFn = any>(
export function checkSchema<Req = Request, Res = any, NextFn = (error?: any => void)>(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants