Skip to content

Commit c3d8f4e

Browse files
markus-perlgustavohenke
authored andcommitted
typescript: move .custom() to shared typings
Closes express-validator#440
1 parent 663a2b7 commit c3d8f4e

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

check/index.d.ts

+17-23
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
1-
import * as express from 'express';
2-
import { Result, Options, Validator } from '../shared-typings';
3-
4-
export const check: ValidationChainBuilder;
5-
export const body: ValidationChainBuilder;
6-
export const cookie: ValidationChainBuilder;
7-
export const header: ValidationChainBuilder;
8-
export const param: ValidationChainBuilder;
9-
export const query: ValidationChainBuilder;
10-
export function validationResult (req: express.Request): Result;
11-
export function oneOf (chains: ValidationChain[]): express.RequestHandler;
12-
13-
export interface ValidationChainBuilder {
14-
(field: string | string[], message?: string): ValidationChain;
15-
}
16-
17-
export interface ValidationChain extends express.RequestHandler, Validator {
18-
custom(validator: CustomValidator): this;
19-
}
20-
21-
export interface CustomValidator {
22-
(value: any, options: { req: express.Request, location: string, path: string }): any;
23-
}
1+
import * as express from 'express';
2+
import { Result, Options, Validator } from '../shared-typings';
3+
4+
export const check: ValidationChainBuilder;
5+
export const body: ValidationChainBuilder;
6+
export const cookie: ValidationChainBuilder;
7+
export const header: ValidationChainBuilder;
8+
export const param: ValidationChainBuilder;
9+
export const query: ValidationChainBuilder;
10+
export function validationResult (req: express.Request): Result;
11+
export function oneOf (chains: ValidationChain[]): express.RequestHandler;
12+
13+
export interface ValidationChainBuilder {
14+
(field: string | string[], message?: string): ValidationChain;
15+
}
16+
17+
export interface ValidationChain extends express.RequestHandler, Validator {}

shared-typings.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as express from 'express';
2+
13
export type URLProtocol = 'http' | 'https' | 'ftp'
24
export type UUIDVersion = 3 | 4 | 5 | 'all'
35
export type IPVersion = 4 | 6
@@ -114,6 +116,7 @@ export interface Validator extends Sanitizer {
114116
equals(equals: any): this;
115117
contains(str: string): this;
116118
matches(pattern: RegExp | string, modifiers?: string): this;
119+
custom(validator: CustomValidator): this;
117120

118121

119122
// Additional ValidatorChain.prototype.* validators
@@ -177,6 +180,10 @@ export interface MappedError {
177180
location: Location
178181
}
179182

183+
export interface CustomValidator {
184+
(value: any, options: { req: express.Request, location: string, path: string }): any;
185+
}
186+
180187
export interface Result {
181188
/**
182189
* @return A boolean determining whether there were errors or not.
@@ -295,6 +302,7 @@ declare namespace Options {
295302
equals?: ValidatorSchemaOptions
296303
contains?: ValidatorSchemaOptions
297304
matches?: ValidatorSchemaOptions
305+
custom?: ValidatorSchemaOptions
298306
}
299307

300308

test/type-definition.spec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ app.use((req: express.Request, res: express.Response, next: express.NextFunction
123123

124124

125125
req.check('param', 'message')
126+
.custom((value, { req, location, path }) => {
127+
throw new Error([value, req.body.foo, location, path].join(' '));
128+
})
126129
.isAwesomeLib()
127130
.isAsync()
128131
.isEmail().isEmail({ allow_display_name: true, allow_utf8_local_part: true, require_tld: true })

0 commit comments

Comments
 (0)