mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-07-07 18:50:14 +00:00
Updating express-validator definitions and adding tests.
This commit is contained in:
40
express-validator/express-validator-tests.ts
Normal file
40
express-validator/express-validator-tests.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
/// <reference path="./express-validator.d.ts"/>
|
||||
|
||||
/* 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);
|
||||
32
express-validator/express-validator.d.ts
vendored
32
express-validator/express-validator.d.ts
vendored
@@ -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 <https://github.com/axefrog/>, Jonathan Häberle <http://dreampulse.de>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
/// <reference path="../express/express.d.ts" />
|
||||
|
||||
|
||||
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<T> { [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> | 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user