Add validate method to Controller (for forms)

This commit is contained in:
Alice Gaudon 2020-04-25 09:33:33 +02:00
parent b401c9732c
commit d676caa3dc

View File

@ -2,6 +2,7 @@ import {RequestHandler, Router} from "express";
import {PathParams} from "express-serve-static-core"; import {PathParams} from "express-serve-static-core";
import config from "config"; import config from "config";
import Logger from "./Logger"; import Logger from "./Logger";
import Validator from "./db/Validator";
export default abstract class Controller { export default abstract class Controller {
private static readonly routes: { [p: string]: string } = {}; private static readonly routes: { [p: string]: string } = {};
@ -115,6 +116,14 @@ export default abstract class Controller {
} }
} }
} }
protected async validate(validationMap: { [p: string]: Validator<any> }, body: any): Promise<void> {
for (const p in validationMap) {
if (validationMap.hasOwnProperty(p)) {
await validationMap[p].execute(p, body[p], false);
}
}
}
} }
export type RouteParams = { [p: string]: string } | string[] | string | number; export type RouteParams = { [p: string]: string } | string[] | string | number;