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
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/chain/validation-chain.ts
Expand Up @@ -5,11 +5,11 @@ import { Validators } from './validators';
import { ContextHandler } from './context-handler';
import { ContextRunner } from './context-runner';

export interface ValidationChain
export interface ValidationChain<Req = Request, Res = any, NextFn = (error?: any) => void>
extends Validators<ValidationChain>,
Sanitizers<ValidationChain>,
ContextHandler<ValidationChain>,
ContextRunner {
(req: Request, res: any, next: (error?: any) => void): void;
(req: Req, res: Res, next: NextFn): void;
builder: ContextBuilder;
}
4 changes: 2 additions & 2 deletions src/middlewares/check.ts
Expand Up @@ -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

fields: string | string[] = '',
locations: Location[] = [],
message?: any,
): ValidationChain {
): ValidationChain<Req, Res, NextFn> {
const builder = new ContextBuilder()
.setFields(Array.isArray(fields) ? fields : [fields])
.setLocations(locations)
Expand Down
11 changes: 9 additions & 2 deletions src/middlewares/schema.ts
Expand Up @@ -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)>(

schema: Schema,
defaultLocations: Location[] = validLocations,
) {
return Object.keys(schema).map(field => {
const config = schema[field];
const chain = check(field, ensureLocations(config, defaultLocations), config.errorMessage);
const chain = check<Req, Res, NextFn>(
field,
ensureLocations(config, defaultLocations),
config.errorMessage,
);

Object.keys(config)
.filter((method: keyof ParamSchema): method is keyof InternalParamSchema => {
Expand Down