mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-08-29 11:12:23 +08:00
Merge pull request #4124 from EricByers/master
express-validator.d.ts updates
This commit is contained in:
@@ -5,11 +5,11 @@
|
||||
import util = require('util')
|
||||
import express = require('express')
|
||||
import expressValidator = require('express-validator')
|
||||
var app = express()
|
||||
var app = express();
|
||||
|
||||
app.use(expressValidator());
|
||||
|
||||
app.post('/:urlparam', function(req: expressValidator.ValidatedRequest, res: express.Response) {
|
||||
app.post('/:urlparam', function(req: express.Request, res: express.Response) {
|
||||
|
||||
// checkBody only checks req.body; none of the other req parameters
|
||||
// Similarly checkParams only checks in req.params (URL params) and
|
||||
@@ -17,6 +17,9 @@ app.post('/:urlparam', function(req: expressValidator.ValidatedRequest, res: exp
|
||||
req.checkBody('postparam', 'Invalid postparam').notEmpty().isInt();
|
||||
req.checkParams('urlparam', 'Invalid urlparam').isAlpha();
|
||||
req.checkQuery('getparam', 'Invalid getparam').isInt();
|
||||
req.checkHeader('testHeader', 'Invalid testHeader').isLowercase().isUppercase();
|
||||
req.checkFiles('testFiles', 'Invalid testFiles').isUrl();
|
||||
|
||||
|
||||
// OR assert can be used to check on all 3 types of params.
|
||||
// req.assert('postparam', 'Invalid postparam').notEmpty().isInt();
|
||||
@@ -24,8 +27,11 @@ app.post('/:urlparam', function(req: expressValidator.ValidatedRequest, res: exp
|
||||
// req.assert('getparam', 'Invalid getparam').isInt();
|
||||
|
||||
req.sanitize('postparam').toBoolean();
|
||||
req.filter('postparam').toBoolean();
|
||||
|
||||
var errors = req.validationErrors();
|
||||
var mappedErrors = req.validationErrors(true);
|
||||
|
||||
if (errors) {
|
||||
res.status(400).send('There have been validation errors: ' + util.inspect(errors));
|
||||
return;
|
||||
|
||||
+172
-169
@@ -5,185 +5,188 @@
|
||||
|
||||
/// <reference path="../express/express.d.ts" />
|
||||
|
||||
// Add RequestValidation Interface on to Express's Request Interface.
|
||||
declare module Express {
|
||||
interface Request extends ExpressValidator.RequestValidation {}
|
||||
}
|
||||
|
||||
// External express-validator module.
|
||||
declare module "express-validator" {
|
||||
import express = require('express');
|
||||
import express = require('express');
|
||||
|
||||
module ExpressValidator {
|
||||
/**
|
||||
*
|
||||
* @middlewareOptions see: https://github.com/ctavan/express-validator#middleware-options
|
||||
*/
|
||||
function ExpressValidator(middlewareOptions?:any):express.RequestHandler;
|
||||
|
||||
export interface ValidationError {
|
||||
msg: string;
|
||||
param: string;
|
||||
}
|
||||
export = ExpressValidator;
|
||||
}
|
||||
|
||||
interface ValidatorFunction { (item: string, message: string): Validator; }
|
||||
interface SanitizerFunction { (item: string): Sanitizer; }
|
||||
interface Dictionary<T> { [key: string]: T; }
|
||||
// Internal Module.
|
||||
declare module ExpressValidator {
|
||||
|
||||
export interface RequestValidation {
|
||||
assert: ValidatorFunction;
|
||||
check: ValidatorFunction;
|
||||
checkBody: ValidatorFunction;
|
||||
checkFiles: ValidatorFunction;
|
||||
checkHeader: ValidatorFunction;
|
||||
checkParams: ValidatorFunction;
|
||||
checkQuery: ValidatorFunction;
|
||||
validate: ValidatorFunction;
|
||||
export interface ValidationError {
|
||||
msg: string;
|
||||
param: string;
|
||||
}
|
||||
|
||||
filter: SanitizerFunction;
|
||||
sanitize: SanitizerFunction;
|
||||
interface ValidatorFunction { (item: string, message: string): Validator; }
|
||||
interface SanitizerFunction { (item: string): Sanitizer; }
|
||||
interface Dictionary<T> { [key: string]: T; }
|
||||
|
||||
onValidationError(errback: (msg: string) => void): void;
|
||||
validationErrors(mapped?: boolean): Dictionary<any> | any[];
|
||||
}
|
||||
export interface RequestValidation {
|
||||
assert: ValidatorFunction;
|
||||
check: ValidatorFunction;
|
||||
checkBody: ValidatorFunction;
|
||||
checkFiles: ValidatorFunction;
|
||||
checkHeader: ValidatorFunction;
|
||||
checkParams: ValidatorFunction;
|
||||
checkQuery: ValidatorFunction;
|
||||
validate: ValidatorFunction;
|
||||
|
||||
/**
|
||||
* Interface for use when using express-validator as middleware for requests
|
||||
*/
|
||||
export interface ValidatedRequest extends express.Request, RequestValidation {}
|
||||
filter: SanitizerFunction;
|
||||
sanitize: SanitizerFunction;
|
||||
|
||||
export interface Validator {
|
||||
/**
|
||||
* Alias for regex()
|
||||
*/
|
||||
is(): Validator;
|
||||
/**
|
||||
* Alias for notRegex()
|
||||
*/
|
||||
not(): Validator;
|
||||
isEmail(): Validator;
|
||||
/**
|
||||
* Accepts http, https, ftp
|
||||
*/
|
||||
isUrl(): Validator;
|
||||
/**
|
||||
* Combines isIPv4 and isIPv6
|
||||
*/
|
||||
isIP(): Validator;
|
||||
isIPv4(): Validator;
|
||||
isIPv6(): Validator;
|
||||
isAlpha(): Validator;
|
||||
isAlphanumeric(): Validator;
|
||||
isNumeric(): Validator;
|
||||
isHexadecimal(): Validator;
|
||||
/**
|
||||
* Accepts valid hexcolors with or without # prefix
|
||||
*/
|
||||
isHexColor(): Validator;
|
||||
/**
|
||||
* isNumeric accepts zero padded numbers, e.g. '001', isInt doesn't
|
||||
*/
|
||||
isInt(): Validator;
|
||||
isLowercase(): Validator;
|
||||
isUppercase(): Validator;
|
||||
isDecimal(): Validator;
|
||||
/**
|
||||
* Alias for isDecimal
|
||||
*/
|
||||
isFloat(): Validator;
|
||||
/**
|
||||
* Check if length is 0
|
||||
*/
|
||||
notNull(): Validator;
|
||||
isNull(): Validator;
|
||||
/**
|
||||
* Not just whitespace (input.trim().length !== 0)
|
||||
*/
|
||||
notEmpty(): Validator;
|
||||
equals(equals:any): Validator;
|
||||
contains(str:string): Validator;
|
||||
notContains(str:string): Validator;
|
||||
/**
|
||||
* Usage: regex(/[a-z]/i) or regex('[a-z]','i')
|
||||
*/
|
||||
regex(pattern:string, modifiers:string): Validator;
|
||||
notRegex(pattern:string, modifiers:string): Validator;
|
||||
/**
|
||||
* max is optional
|
||||
*/
|
||||
len(min:number, max?:number): Validator;
|
||||
/**
|
||||
* Version can be 3, 4 or 5 or empty, see http://en.wikipedia.org/wiki/Universally_unique_identifier
|
||||
*/
|
||||
isUUID(version:number): Validator;
|
||||
/**
|
||||
* Alias for isUUID(3)
|
||||
*/
|
||||
isUUIDv3(): Validator;
|
||||
/**
|
||||
* Alias for isUUID(4)
|
||||
*/
|
||||
isUUIDv4(): Validator;
|
||||
/**
|
||||
* Alias for isUUID(5)
|
||||
*/
|
||||
isUUIDv5(): Validator;
|
||||
/**
|
||||
* Uses Date.parse() - regex is probably a better choice
|
||||
*/
|
||||
isDate(): Validator;
|
||||
/**
|
||||
* Argument is optional and defaults to today. Comparison is non-inclusive
|
||||
*/
|
||||
isAfter(date:Date): Validator;
|
||||
/**
|
||||
* Argument is optional and defaults to today. Comparison is non-inclusive
|
||||
*/
|
||||
isBefore(date:Date): Validator;
|
||||
isIn(options:string): Validator;
|
||||
isIn(options:string[]): Validator;
|
||||
notIn(options:string): Validator;
|
||||
notIn(options:string[]): Validator;
|
||||
max(val:string): Validator;
|
||||
min(val:string): Validator;
|
||||
/**
|
||||
* Will work against Visa, MasterCard, American Express, Discover, Diners Club, and JCB card numbering formats
|
||||
*/
|
||||
isCreditCard(): Validator;
|
||||
}
|
||||
onValidationError(errback: (msg: string) => void): void;
|
||||
validationErrors(mapped?: boolean): Dictionary<any> | any[];
|
||||
}
|
||||
|
||||
interface Sanitizer {
|
||||
/**
|
||||
* Trim optional `chars`, default is to trim whitespace (\r\n\t )
|
||||
*/
|
||||
trim(...chars:string[]): Sanitizer;
|
||||
ltrim(...chars:string[]): Sanitizer;
|
||||
rtrim(...chars:string[]): Sanitizer;
|
||||
ifNull(replace:any): Sanitizer;
|
||||
toFloat(): Sanitizer;
|
||||
toInt(): Sanitizer;
|
||||
/**
|
||||
* True unless str = '0', 'false', or str.length == 0
|
||||
*/
|
||||
toBoolean(): Sanitizer;
|
||||
/**
|
||||
* False unless str = '1' or 'true'
|
||||
*/
|
||||
toBooleanStrict(): Sanitizer;
|
||||
/**
|
||||
* Decode HTML entities
|
||||
*/
|
||||
entityDecode(): Sanitizer;
|
||||
entityEncode(): Sanitizer;
|
||||
/**
|
||||
* Escape &, <, >, and "
|
||||
*/
|
||||
escape(): Sanitizer;
|
||||
/**
|
||||
* Remove common XSS attack vectors from user-supplied HTML
|
||||
*/
|
||||
xss(): Sanitizer;
|
||||
/**
|
||||
* Remove common XSS attack vectors from images
|
||||
*/
|
||||
xss(fromImages:boolean): Sanitizer;
|
||||
}
|
||||
}
|
||||
export interface Validator {
|
||||
/**
|
||||
* Alias for regex()
|
||||
*/
|
||||
is(): Validator;
|
||||
/**
|
||||
* Alias for notRegex()
|
||||
*/
|
||||
not(): Validator;
|
||||
isEmail(): Validator;
|
||||
/**
|
||||
* Accepts http, https, ftp
|
||||
*/
|
||||
isUrl(): Validator;
|
||||
/**
|
||||
* Combines isIPv4 and isIPv6
|
||||
*/
|
||||
isIP(): Validator;
|
||||
isIPv4(): Validator;
|
||||
isIPv6(): Validator;
|
||||
isAlpha(): Validator;
|
||||
isAlphanumeric(): Validator;
|
||||
isNumeric(): Validator;
|
||||
isHexadecimal(): Validator;
|
||||
/**
|
||||
* Accepts valid hexcolors with or without # prefix
|
||||
*/
|
||||
isHexColor(): Validator;
|
||||
/**
|
||||
* isNumeric accepts zero padded numbers, e.g. '001', isInt doesn't
|
||||
*/
|
||||
isInt(): Validator;
|
||||
isLowercase(): Validator;
|
||||
isUppercase(): Validator;
|
||||
isDecimal(): Validator;
|
||||
/**
|
||||
* Alias for isDecimal
|
||||
*/
|
||||
isFloat(): Validator;
|
||||
/**
|
||||
* Check if length is 0
|
||||
*/
|
||||
notNull(): Validator;
|
||||
isNull(): Validator;
|
||||
/**
|
||||
* Not just whitespace (input.trim().length !== 0)
|
||||
*/
|
||||
notEmpty(): Validator;
|
||||
equals(equals:any): Validator;
|
||||
contains(str:string): Validator;
|
||||
notContains(str:string): Validator;
|
||||
/**
|
||||
* Usage: regex(/[a-z]/i) or regex('[a-z]','i')
|
||||
*/
|
||||
regex(pattern:string, modifiers:string): Validator;
|
||||
notRegex(pattern:string, modifiers:string): Validator;
|
||||
/**
|
||||
* max is optional
|
||||
*/
|
||||
len(min:number, max?:number): Validator;
|
||||
/**
|
||||
* Version can be 3, 4 or 5 or empty, see http://en.wikipedia.org/wiki/Universally_unique_identifier
|
||||
*/
|
||||
isUUID(version:number): Validator;
|
||||
/**
|
||||
* Alias for isUUID(3)
|
||||
*/
|
||||
isUUIDv3(): Validator;
|
||||
/**
|
||||
* Alias for isUUID(4)
|
||||
*/
|
||||
isUUIDv4(): Validator;
|
||||
/**
|
||||
* Alias for isUUID(5)
|
||||
*/
|
||||
isUUIDv5(): Validator;
|
||||
/**
|
||||
* Uses Date.parse() - regex is probably a better choice
|
||||
*/
|
||||
isDate(): Validator;
|
||||
/**
|
||||
* Argument is optional and defaults to today. Comparison is non-inclusive
|
||||
*/
|
||||
isAfter(date:Date): Validator;
|
||||
/**
|
||||
* Argument is optional and defaults to today. Comparison is non-inclusive
|
||||
*/
|
||||
isBefore(date:Date): Validator;
|
||||
isIn(options:string): Validator;
|
||||
isIn(options:string[]): Validator;
|
||||
notIn(options:string): Validator;
|
||||
notIn(options:string[]): Validator;
|
||||
max(val:string): Validator;
|
||||
min(val:string): Validator;
|
||||
/**
|
||||
* Will work against Visa, MasterCard, American Express, Discover, Diners Club, and JCB card numbering formats
|
||||
*/
|
||||
isCreditCard(): Validator;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @middlewareOptions see: https://github.com/ctavan/express-validator#middleware-options
|
||||
*/
|
||||
function ExpressValidator(middlewareOptions?:any):express.RequestHandler;
|
||||
interface Sanitizer {
|
||||
/**
|
||||
* Trim optional `chars`, default is to trim whitespace (\r\n\t )
|
||||
*/
|
||||
trim(...chars:string[]): Sanitizer;
|
||||
ltrim(...chars:string[]): Sanitizer;
|
||||
rtrim(...chars:string[]): Sanitizer;
|
||||
ifNull(replace:any): Sanitizer;
|
||||
toFloat(): Sanitizer;
|
||||
toInt(): Sanitizer;
|
||||
/**
|
||||
* True unless str = '0', 'false', or str.length == 0
|
||||
*/
|
||||
toBoolean(): Sanitizer;
|
||||
/**
|
||||
* False unless str = '1' or 'true'
|
||||
*/
|
||||
toBooleanStrict(): Sanitizer;
|
||||
/**
|
||||
* Decode HTML entities
|
||||
*/
|
||||
entityDecode(): Sanitizer;
|
||||
entityEncode(): Sanitizer;
|
||||
/**
|
||||
* Escape &, <, >, and "
|
||||
*/
|
||||
escape(): Sanitizer;
|
||||
/**
|
||||
* Remove common XSS attack vectors from user-supplied HTML
|
||||
*/
|
||||
xss(): Sanitizer;
|
||||
/**
|
||||
* Remove common XSS attack vectors from images
|
||||
*/
|
||||
xss(fromImages:boolean): Sanitizer;
|
||||
}
|
||||
|
||||
export = ExpressValidator;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user