From c1068be8f1b975e4d642c108f99a0a4ed93097ea Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 23 Nov 2019 14:38:48 +0100 Subject: [PATCH] Can safely get an integer --- src/entities/RequestHandler.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/entities/RequestHandler.ts b/src/entities/RequestHandler.ts index 73562ba..b4a750c 100644 --- a/src/entities/RequestHandler.ts +++ b/src/entities/RequestHandler.ts @@ -66,6 +66,29 @@ export class RequestHandler { return email; } + /** + * Get an integer included in the request + * + * @param name Name of POST field + * @param fallback Fallback value (if none, throw an error) + * @returns The number (throws in case of error) + */ + public postInt(name: string, fallback ?: number) : number { + const param = this.getPostParam(name); + + if(param == undefined) { + if(!fallback) + this.error(400, "Missing integer '"+name+"' in the request!"); + return fallback; + } + + // Check number + if(Number.parseInt(param).toString() !== param.toString()) + this.error(400, "'"+name+"' is an invalid integer!"); + + return Number.parseInt(param); + } + /** * Validate API tokens