mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-18 15:00:00 +00:00
Fix API config validation not taking default options into account
This commit is contained in:
parent
a3f423a1d6
commit
a2d5bb51a7
3 changed files with 8 additions and 4 deletions
|
@ -76,12 +76,14 @@ export function initGuildsAPI(app: express.Express) {
|
||||||
parsedConfig = {};
|
parsedConfig = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
const errors = validateGuildConfig(parsedConfig);
|
const errors = await validateGuildConfig(parsedConfig);
|
||||||
|
|
||||||
if (errors) {
|
if (errors) {
|
||||||
return res.status(422).json({ errors });
|
return res.status(422).json({ errors });
|
||||||
}
|
}
|
||||||
|
|
||||||
await configs.saveNewRevision(`guild-${req.params.guildId}`, config, req.user.userId);
|
await configs.saveNewRevision(`guild-${req.params.guildId}`, config, req.user.userId);
|
||||||
|
|
||||||
ok(res);
|
ok(res);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { guildPlugins } from "./plugins/availablePlugins";
|
||||||
import { decodeAndValidateStrict, StrictValidationError } from "./validatorUtils";
|
import { decodeAndValidateStrict, StrictValidationError } from "./validatorUtils";
|
||||||
import { ZeppelinPlugin } from "./plugins/ZeppelinPlugin";
|
import { ZeppelinPlugin } from "./plugins/ZeppelinPlugin";
|
||||||
import { IZeppelinGuildConfig } from "./types";
|
import { IZeppelinGuildConfig } from "./types";
|
||||||
import { PluginOptions } from "knub";
|
import { configUtils, PluginOptions } from "knub";
|
||||||
|
|
||||||
const pluginNameToPlugin = new Map<string, ZeppelinPlugin>();
|
const pluginNameToPlugin = new Map<string, ZeppelinPlugin>();
|
||||||
for (const plugin of guildPlugins) {
|
for (const plugin of guildPlugins) {
|
||||||
|
@ -26,7 +26,7 @@ const globalConfigRootSchema = t.type({
|
||||||
|
|
||||||
const partialMegaTest = t.partial({ name: t.string });
|
const partialMegaTest = t.partial({ name: t.string });
|
||||||
|
|
||||||
export function validateGuildConfig(config: any): string[] | null {
|
export async function validateGuildConfig(config: any): Promise<string[] | null> {
|
||||||
const validationResult = decodeAndValidateStrict(partialGuildConfigRootSchema, config);
|
const validationResult = decodeAndValidateStrict(partialGuildConfigRootSchema, config);
|
||||||
if (validationResult instanceof StrictValidationError) return validationResult.getErrors();
|
if (validationResult instanceof StrictValidationError) return validationResult.getErrors();
|
||||||
|
|
||||||
|
@ -40,7 +40,8 @@ export function validateGuildConfig(config: any): string[] | null {
|
||||||
|
|
||||||
const plugin = pluginNameToPlugin.get(pluginName);
|
const plugin = pluginNameToPlugin.get(pluginName);
|
||||||
try {
|
try {
|
||||||
plugin.configPreprocessor(pluginOptions as PluginOptions<any>);
|
const mergedOptions = configUtils.mergeConfig(plugin.defaultOptions || {}, pluginOptions);
|
||||||
|
await plugin.configPreprocessor(mergedOptions as PluginOptions<any>);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof StrictValidationError) {
|
if (err instanceof StrictValidationError) {
|
||||||
return err.getErrors().map(err => {
|
return err.getErrors().map(err => {
|
||||||
|
|
|
@ -40,6 +40,7 @@ export function getPluginConfigPreprocessor(
|
||||||
? decodeAndValidateStrict(blueprint.configSchema, options.config)
|
? decodeAndValidateStrict(blueprint.configSchema, options.config)
|
||||||
: options.config;
|
: options.config;
|
||||||
if (decodedConfig instanceof StrictValidationError) {
|
if (decodedConfig instanceof StrictValidationError) {
|
||||||
|
console.log("o", options);
|
||||||
throw decodedConfig;
|
throw decodedConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue