From 2f40fd4aa0aaf9a71949d86f093349c9409ea0b8 Mon Sep 17 00:00:00 2001 From: takejohn <105504345+takejohn@users.noreply.github.com> Date: Sat, 30 Mar 2024 00:57:58 +0900 Subject: [PATCH] =?UTF-8?q?config=E3=81=AE=E5=9E=8B=E3=83=81=E3=82=A7?= =?UTF-8?q?=E3=83=83=E3=82=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config.ts | 67 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/src/config.ts b/src/config.ts index aac43af..c307ef2 100644 --- a/src/config.ts +++ b/src/config.ts @@ -18,15 +18,72 @@ type Config = { }; import uncheckedConfig from '../config.json' assert { type: 'json' }; +import log from '@/utils/log.js'; + +class Type { + public static readonly string = new Type('string'); + public static readonly boolean = new Type('boolean'); + + public readonly name: string; + + private constructor(name: string) { + this.name = name; + } + + check(value: unknown): value is T { + return typeof value == this.name; + } +} + +function checkProperty(config: Object, key: K, type: Type): config is { [J in K]: Config[K] } { + const result = key in config && type.check(config[key as string]); + if (!result) { + log(`config.json: Property ${key}: ${type.name} required`); + } + return result; +} + +function checkOptionalProperty(config: Object, key: K, type: Type): config is { [J in K]?: Config[K] } { + if (!(key in config)) { + return true; + } + const result = type.check(config[key as string]); + if (!result) { + log(`config.json: The type of property ${key} must be ${type.name}`); + } + return result; +} + +function setProperty(config: Object, key: K, value: Config[K]): asserts config is { [L in K]: Config[K] } { + (config as { [L in K]?: Config[K] })[key] = value; +} function validate(config: unknown): Config { - // TODO: as を使わずにしっかりと検証を行う - return config as Config; + if (!(config instanceof Object)) { + log('config.json: Root object required'); + } else if ( + checkProperty(config, 'host', Type.string) && + checkOptionalProperty(config, 'serverName', Type.string) && + checkProperty(config, 'i', Type.string) && + checkOptionalProperty(config, 'master', Type.string) && + checkProperty(config, 'keywordEnabled', Type.boolean) && + checkProperty(config, 'reversiEnabled', Type.boolean) && + checkProperty(config, 'notingEnabled', Type.boolean) && + checkProperty(config, 'chartEnabled', Type.boolean) && + checkProperty(config, 'serverMonitoring', Type.boolean) && + checkOptionalProperty(config, 'checkEmojisEnabled', Type.boolean) && + checkOptionalProperty(config, 'checkEmojisAtOnce', Type.boolean) && + checkOptionalProperty(config, 'mecab', Type.string) && + checkOptionalProperty(config, 'mecabDic', Type.string) && + checkOptionalProperty(config, 'memoryDir', Type.string) + ) { + setProperty(config, 'wsUrl', config.host.replace('http', 'ws')); + setProperty(config, 'apiUrl', config.host + '/api'); + return config; + } + throw new TypeError('config.json has an invalid type'); } const config = validate(uncheckedConfig); -config.wsUrl = config.host.replace('http', 'ws'); -config.apiUrl = config.host + '/api'; - export default config;