From d676caa3dc94bc581bc1e2a543f19641e01bbf24 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sat, 25 Apr 2020 09:33:33 +0200 Subject: [PATCH] Add validate method to Controller (for forms) --- src/Controller.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Controller.ts b/src/Controller.ts index 9304438..d9ab8ab 100644 --- a/src/Controller.ts +++ b/src/Controller.ts @@ -2,6 +2,7 @@ import {RequestHandler, Router} from "express"; import {PathParams} from "express-serve-static-core"; import config from "config"; import Logger from "./Logger"; +import Validator from "./db/Validator"; export default abstract class Controller { private static readonly routes: { [p: string]: string } = {}; @@ -115,6 +116,14 @@ export default abstract class Controller { } } } + + protected async validate(validationMap: { [p: string]: Validator }, body: any): Promise { + 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; \ No newline at end of file