mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-18 15:45:03 +00:00
Switch from ajv to io-ts for config validation; validate configs on save in the API/dashboard; start work on creating io-ts schemas for all plugins
This commit is contained in:
parent
1ecce52973
commit
3f4724b699
14 changed files with 256 additions and 41 deletions
51
src/configValidator.ts
Normal file
51
src/configValidator.ts
Normal file
|
@ -0,0 +1,51 @@
|
|||
import * as t from "io-ts";
|
||||
import { IPluginOptions } from "knub";
|
||||
import { pipe } from "fp-ts/lib/pipeable";
|
||||
import { fold } from "fp-ts/lib/Either";
|
||||
import { PathReporter } from "io-ts/lib/PathReporter";
|
||||
import { availablePlugins } from "./plugins/availablePlugins";
|
||||
import { ZeppelinPlugin } from "./plugins/ZeppelinPlugin";
|
||||
import { validateStrict } from "./validatorUtils";
|
||||
|
||||
const pluginNameToClass = new Map<string, typeof ZeppelinPlugin>();
|
||||
for (const pluginClass of availablePlugins) {
|
||||
// @ts-ignore
|
||||
pluginNameToClass.set(pluginClass.pluginName, pluginClass);
|
||||
}
|
||||
|
||||
const guildConfigRootSchema = t.type({
|
||||
prefix: t.string,
|
||||
levels: t.record(t.string, t.number),
|
||||
plugins: t.record(t.string, t.unknown),
|
||||
});
|
||||
const partialGuildConfigRootSchema = t.exact(t.partial(guildConfigRootSchema.props));
|
||||
|
||||
const globalConfigRootSchema = t.type({
|
||||
url: t.string,
|
||||
owners: t.array(t.string),
|
||||
plugins: t.record(t.string, t.unknown),
|
||||
});
|
||||
|
||||
const partialMegaTest = t.partial({ name: t.string });
|
||||
|
||||
export function validateGuildConfig(config: any): string[] | null {
|
||||
const rootErrors = validateStrict(partialGuildConfigRootSchema, config);
|
||||
if (rootErrors) return rootErrors;
|
||||
|
||||
if (config.plugins) {
|
||||
for (const [pluginName, pluginOptions] of Object.entries(config.plugins)) {
|
||||
if (!pluginNameToClass.has(pluginName)) {
|
||||
return [`Unknown plugin: ${pluginName}`];
|
||||
}
|
||||
|
||||
const pluginClass = pluginNameToClass.get(pluginName);
|
||||
let pluginErrors = pluginClass.validateOptions(pluginOptions);
|
||||
if (pluginErrors) {
|
||||
pluginErrors = pluginErrors.map(err => `${pluginName}: ${err}`);
|
||||
return pluginErrors;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue