From 561a3d28aa5a1d6262dc23f801ead01789ffb6b8 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Thu, 24 Dec 2020 21:57:23 -0700 Subject: [PATCH] Add code to validate different data --- server/src/utils/validate.ts | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 server/src/utils/validate.ts diff --git a/server/src/utils/validate.ts b/server/src/utils/validate.ts new file mode 100644 index 0000000..7afe20d --- /dev/null +++ b/server/src/utils/validate.ts @@ -0,0 +1,45 @@ +import { log } from "../main"; + +export class Validate { + public static config(conf: config) { + + let valid = true; + + // Assert data in log object: + if (![`silly`,`debug`,`info`,`error`,`fatal`,`warn`,`trace`].includes(conf.log.level)) { + log.error(`Unknown log level: ${conf.log.level}`); + valid = false; + }; + + // Assert data in the game object + if (![`csv`].includes(conf.game.cards.type)) { + log.error(`Unsupported cards type: ${conf.game.cards.type}`); + valid = false; + }; + + if (conf.game.cards.type == `sheets` && !conf.game.cards.key) { + log.error(`Cannot have game.cards.type set to "sheets" and not have the game.cards.key set.`); + valid = false; + }; + + if (conf.game.code_length <= 1) { + log.error(`Game codes can't have <= 1 characters: ${conf.game.code_length}`); + valid = false; + } + + // Assert data in the web server object + if (conf.webserver.enabled) { + if (!conf.webserver.port) { + log.error(`Invalid webserver port value: ${conf.webserver.port}`); + valid = false; + }; + }; + if (!conf.webserver.hostname) { + log.error(`Can't have a blank or null webserver.hostname`); + valid = false; + }; + + // Config is valid + return valid; + } +} \ No newline at end of file