diff --git a/express-validator/express-validator-tests.ts b/express-validator/express-validator-tests.ts new file mode 100644 index 0000000000..8d1362edf8 --- /dev/null +++ b/express-validator/express-validator-tests.ts @@ -0,0 +1,40 @@ +/// + +/* Usage Example from https://github.com/ctavan/express-validator */ + +import util = require('util') +import express = require('express') +import expressValidator = require('express-validator') +var app = express() + +app.use(expressValidator()); + +app.post('/:urlparam', function(req: expressValidator.ValidatedRequest, res) { + + // checkBody only checks req.body; none of the other req parameters + // Similarly checkParams only checks in req.params (URL params) and + // checkQuery only checks req.query (GET params). + req.checkBody('postparam', 'Invalid postparam').notEmpty().isInt(); + req.checkParams('urlparam', 'Invalid urlparam').isAlpha(); + req.checkQuery('getparam', 'Invalid getparam').isInt(); + + // OR assert can be used to check on all 3 types of params. + // req.assert('postparam', 'Invalid postparam').notEmpty().isInt(); + // req.assert('urlparam', 'Invalid urlparam').isAlpha(); + // req.assert('getparam', 'Invalid getparam').isInt(); + + req.sanitize('postparam').toBoolean(); + + var errors = req.validationErrors(); + if (errors) { + res.send('There have been validation errors: ' + util.inspect(errors), 400); + return; + } + res.json({ + urlparam: req.param('urlparam'), + getparam: req.param('getparam'), + postparam: req.param('postparam') + }); +}); + +app.listen(8888); diff --git a/express-validator/express-validator.d.ts b/express-validator/express-validator.d.ts index 5a5df2fa0d..c2e5aa348f 100644 --- a/express-validator/express-validator.d.ts +++ b/express-validator/express-validator.d.ts @@ -1,11 +1,10 @@ -// Type definitions for express-validator +// Type definitions for express-validator 2.9.0 // Project: https://github.com/ctavan/express-validator // Definitions by: Nathan Ridley , Jonathan Häberle // Definitions: https://github.com/borisyankov/DefinitelyTyped /// - declare module "express-validator" { import express = require('express'); @@ -16,14 +15,32 @@ declare module "express-validator" { param: string; } + interface ValidatorFunction { (item: string, message: string): Validator; } + interface SanatizerFunction { (item: string): Sanitizer; } + interface Dictionary { [key: string]: T; } + export interface RequestValidation { - check(field:string, message:string): Validator; - assert(field:string, message:string): Validator; - sanitize(field:string): Sanitizer; - onValidationError(func:(msg:string) => void): void; - validationErrors() : any; + assert: ValidatorFunction; + check: ValidatorFunction; + checkBody: ValidatorFunction; + checkFiles: ValidatorFunction; + checkHeader: ValidatorFunction; + checkParams: ValidatorFunction; + checkQuery: ValidatorFunction; + validate: ValidatorFunction; + + filter: SanatizerFunction; + sanitize: SanatizerFunction; + + onValidationError(errback: (msg: string) => void): void; + validationErrors(mapped?: boolean): Dictionary | any[]; } + /** + * Interface for use when using express-validator as middleware for requests + */ + export interface ValidatedRequest extends express.Request, RequestValidation {} + export interface Validator { /** * Alias for regex() @@ -168,6 +185,5 @@ declare module "express-validator" { */ function ExpressValidator(middlewareOptions?:any):express.RequestHandler; - export = ExpressValidator; }