Initial commit

This commit is contained in:
Rodrigo Pinto 2020-02-18 22:53:38 -05:00
commit d7af9332c1
1674 changed files with 119641 additions and 0 deletions

View file

@ -0,0 +1,3 @@
import { ValidationChain } from '../chain';
import { Location } from '../base';
export declare function check(fields?: string | string[], locations?: Location[], message?: any): ValidationChain;

View file

@ -0,0 +1,31 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const chain_1 = require("../chain");
const utils_1 = require("../utils");
const context_builder_1 = require("../context-builder");
function check(fields = '', locations = [], message) {
const builder = new context_builder_1.ContextBuilder()
.setFields(Array.isArray(fields) ? fields : [fields])
.setLocations(locations)
.setMessage(message);
const runner = new chain_1.ContextRunnerImpl(builder);
const middleware = (req, _res, next) => __awaiter(this, void 0, void 0, function* () {
try {
yield runner.run(req);
next();
}
catch (e) {
next(e);
}
});
return Object.assign(middleware, utils_1.bindAll(runner), utils_1.bindAll(new chain_1.SanitizersImpl(builder, middleware)), utils_1.bindAll(new chain_1.ValidatorsImpl(builder, middleware)), utils_1.bindAll(new chain_1.ContextHandlerImpl(builder, middleware)), { builder });
}
exports.check = check;

View file

@ -0,0 +1,7 @@
import { ValidationChain } from '../chain';
import { Middleware, Request } from '../base';
export declare type OneOfCustomMessageBuilder = (options: {
req: Request;
}) => any;
export declare function oneOf(chains: (ValidationChain | ValidationChain[])[], message?: OneOfCustomMessageBuilder): Middleware;
export declare function oneOf(chains: (ValidationChain | ValidationChain[])[], message?: any): Middleware;

View file

@ -0,0 +1,46 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const _ = require("lodash");
const base_1 = require("../base");
const context_builder_1 = require("../context-builder");
function oneOf(chains, message) {
return (req, _res, next) => __awaiter(this, void 0, void 0, function* () {
const surrogateContext = new context_builder_1.ContextBuilder().build();
// Run each group of chains in parallel, and within each group, run each chain in parallel too.
const promises = chains.map((chain) => __awaiter(this, void 0, void 0, function* () {
const group = Array.isArray(chain) ? chain : [chain];
const contexts = yield Promise.all(group.map(chain => chain.run(req, { saveContext: false })));
const groupErrors = _.flatMap(contexts, 'errors');
// #536: The data from a chain within oneOf() can only be made available to e.g. matchedData()
// if its entire group is valid.
if (!groupErrors.length) {
contexts.forEach(context => {
surrogateContext.addFieldInstances(context.getData());
});
}
return groupErrors;
}));
req[base_1.contextsSymbol] = (req[base_1.contextsSymbol] || []).concat(surrogateContext);
try {
const allErrors = yield Promise.all(promises);
const success = allErrors.some(groupErrors => groupErrors.length === 0);
if (!success) {
// Only add an error to the context if no group of chains had success.
surrogateContext.addError(typeof message === 'function' ? message({ req }) : message || 'Invalid value(s)', _.flatMap(allErrors));
}
next();
}
catch (e) {
next(e);
}
});
}
exports.oneOf = oneOf;

View file

@ -0,0 +1,7 @@
import { Location } from '../base';
export declare function buildSanitizeFunction(locations: Location[]): (fields?: string | string[] | undefined) => import("..").SanitizationChain;
export declare const sanitize: (fields?: string | string[] | undefined) => import("..").SanitizationChain;
export declare const sanitizeBody: (fields?: string | string[] | undefined) => import("..").SanitizationChain;
export declare const sanitizeCookie: (fields?: string | string[] | undefined) => import("..").SanitizationChain;
export declare const sanitizeParam: (fields?: string | string[] | undefined) => import("..").SanitizationChain;
export declare const sanitizeQuery: (fields?: string | string[] | undefined) => import("..").SanitizationChain;

View file

@ -0,0 +1,12 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const sanitize_1 = require("./sanitize");
function buildSanitizeFunction(locations) {
return (fields) => sanitize_1.sanitize(fields, locations);
}
exports.buildSanitizeFunction = buildSanitizeFunction;
exports.sanitize = buildSanitizeFunction(['body', 'cookies', 'params', 'query']);
exports.sanitizeBody = buildSanitizeFunction(['body']);
exports.sanitizeCookie = buildSanitizeFunction(['cookies']);
exports.sanitizeParam = buildSanitizeFunction(['params']);
exports.sanitizeQuery = buildSanitizeFunction(['query']);

View file

@ -0,0 +1,3 @@
import { SanitizationChain } from '../chain';
import { Location } from '../base';
export declare function sanitize(fields?: string | string[], locations?: Location[]): SanitizationChain;

View file

@ -0,0 +1,30 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const chain_1 = require("../chain");
const utils_1 = require("../utils");
const context_builder_1 = require("../context-builder");
function sanitize(fields = '', locations = []) {
const builder = new context_builder_1.ContextBuilder()
.setFields(Array.isArray(fields) ? fields : [fields])
.setLocations(locations);
const runner = new chain_1.ContextRunnerImpl(builder);
const middleware = (req, _res, next) => __awaiter(this, void 0, void 0, function* () {
try {
yield runner.run(req);
next();
}
catch (e) {
next(e);
}
});
return Object.assign(middleware, utils_1.bindAll(runner), utils_1.bindAll(new chain_1.SanitizersImpl(builder, middleware)), { builder });
}
exports.sanitize = sanitize;

View file

@ -0,0 +1,44 @@
import { Sanitizers } from '../chain/sanitizers';
import { Validators } from '../chain/validators';
import { DynamicMessageCreator, Location } from '../base';
import { Optional } from '../context';
declare type ValidatorSchemaOptions<K extends keyof Validators<any>> = true | {
options?: Parameters<Validators<any>[K]> | Parameters<Validators<any>[K]>[0];
errorMessage?: DynamicMessageCreator | any;
negated?: boolean;
};
export declare type ValidatorsSchema = {
[K in keyof Validators<any>]?: ValidatorSchemaOptions<K>;
};
declare type SanitizerSchemaOptions<K extends keyof Sanitizers<any>> = true | {
options?: Parameters<Sanitizers<any>[K]> | Parameters<Sanitizers<any>[K]>[0];
};
export declare type SanitizersSchema = {
[K in keyof Sanitizers<any>]?: SanitizerSchemaOptions<K>;
};
declare type InternalParamSchema = ValidatorsSchema & SanitizersSchema;
/**
* Defines a schema of validations/sanitizations plus a general validation error message
* and possible field locations.
*/
export declare type ParamSchema = InternalParamSchema & {
in?: Location | Location[];
errorMessage?: DynamicMessageCreator | any;
optional?: true | {
options?: Partial<Optional>;
};
};
/**
* @deprecated Only here for v5 compatibility. Please use ParamSchema instead.
*/
export declare type ValidationParamSchema = ParamSchema;
/**
* Defines a mapping from field name to a validations/sanitizations schema.
*/
export declare type Schema = Record<string, ParamSchema>;
/**
* @deprecated Only here for v5 compatibility. Please use Schema instead.
*/
export declare type ValidationSchema = Schema;
export declare function checkSchema(schema: Schema, defaultLocations?: Location[]): import("../chain").ValidationChain[];
export {};

View file

@ -0,0 +1,49 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const chain_1 = require("../chain");
const check_1 = require("./check");
const validLocations = ['body', 'cookies', 'headers', 'params', 'query'];
const protectedNames = ['errorMessage', 'in'];
function checkSchema(schema, defaultLocations = validLocations) {
return Object.keys(schema).map(field => {
const config = schema[field];
const chain = check_1.check(field, ensureLocations(config, defaultLocations), config.errorMessage);
Object.keys(config)
.filter((method) => {
return config[method] && !protectedNames.includes(method);
})
.forEach(method => {
if (typeof chain[method] !== 'function') {
console.warn(`express-validator: a validator/sanitizer with name ${method} does not exist`);
return;
}
// Using "!" because typescript doesn't know it isn't undefined.
const methodCfg = config[method];
let options = methodCfg === true ? [] : methodCfg.options || [];
if (options != null && !Array.isArray(options)) {
options = [options];
}
if (isValidatorOptions(method, methodCfg) && methodCfg.negated) {
chain.not();
}
chain[method](...options);
if (isValidatorOptions(method, methodCfg) && methodCfg.errorMessage) {
chain.withMessage(methodCfg.errorMessage);
}
});
return chain;
});
}
exports.checkSchema = checkSchema;
function isValidatorOptions(method, methodCfg) {
return methodCfg !== true && method in chain_1.ValidatorsImpl.prototype;
}
function ensureLocations(config, defaults) {
// .filter(Boolean) is done because in can be undefined -- which is not going away from the type
// See https://github.com/Microsoft/TypeScript/pull/29955 for details
const locations = Array.isArray(config.in)
? config.in
: [config.in].filter(Boolean);
const actualLocations = locations.length ? locations : defaults;
return actualLocations.filter(location => validLocations.includes(location));
}

View file

@ -0,0 +1,8 @@
import { Location } from '../base';
export declare function buildCheckFunction(locations: Location[]): (fields?: string | string[] | undefined, message?: any) => import("..").ValidationChain;
export declare const check: (fields?: string | string[] | undefined, message?: any) => import("..").ValidationChain;
export declare const body: (fields?: string | string[] | undefined, message?: any) => import("..").ValidationChain;
export declare const cookie: (fields?: string | string[] | undefined, message?: any) => import("..").ValidationChain;
export declare const header: (fields?: string | string[] | undefined, message?: any) => import("..").ValidationChain;
export declare const param: (fields?: string | string[] | undefined, message?: any) => import("..").ValidationChain;
export declare const query: (fields?: string | string[] | undefined, message?: any) => import("..").ValidationChain;

View file

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const check_1 = require("./check");
function buildCheckFunction(locations) {
return (fields, message) => check_1.check(fields, locations, message);
}
exports.buildCheckFunction = buildCheckFunction;
exports.check = buildCheckFunction(['body', 'cookies', 'headers', 'params', 'query']);
exports.body = buildCheckFunction(['body']);
exports.cookie = buildCheckFunction(['cookies']);
exports.header = buildCheckFunction(['headers']);
exports.param = buildCheckFunction(['params']);
exports.query = buildCheckFunction(['query']);