3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-15 05:41:51 +00:00

fix: broken config regex parsing

This commit is contained in:
Dragory 2023-04-02 03:18:55 +03:00
parent d231c72a5b
commit 9d4e9cf364
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
8 changed files with 29 additions and 54 deletions

View file

@ -25,7 +25,7 @@ import { isStaff } from "./staff";
import { TZeppelinKnub } from "./types"; import { TZeppelinKnub } from "./types";
import { errorMessage, successMessage, tNullable } from "./utils"; import { errorMessage, successMessage, tNullable } from "./utils";
import { Tail } from "./utils/typeUtils"; import { Tail } from "./utils/typeUtils";
import { StrictValidationError, validate } from "./validatorUtils"; import { parseIoTsSchema, StrictValidationError } from "./validatorUtils";
const { getMemberLevel } = helpers; const { getMemberLevel } = helpers;
@ -110,11 +110,14 @@ export function strictValidationErrorToConfigValidationError(err: StrictValidati
export function makeIoTsConfigParser<Schema extends t.Type<any>>(schema: Schema): (input: unknown) => t.TypeOf<Schema> { export function makeIoTsConfigParser<Schema extends t.Type<any>>(schema: Schema): (input: unknown) => t.TypeOf<Schema> {
return (input: unknown) => { return (input: unknown) => {
const error = validate(schema, input); try {
if (error) { return parseIoTsSchema(schema, input);
throw strictValidationErrorToConfigValidationError(error); } catch (err) {
if (err instanceof StrictValidationError) {
throw strictValidationErrorToConfigValidationError(err);
}
throw err;
} }
return input as t.TypeOf<Schema>;
}; };
} }

View file

@ -1,4 +1,3 @@
import * as t from "io-ts";
import { configUtils, CooldownManager } from "knub"; import { configUtils, CooldownManager } from "knub";
import { GuildAntiraidLevels } from "../../data/GuildAntiraidLevels"; import { GuildAntiraidLevels } from "../../data/GuildAntiraidLevels";
import { GuildArchives } from "../../data/GuildArchives"; import { GuildArchives } from "../../data/GuildArchives";
@ -9,7 +8,7 @@ import { discardRegExpRunner, getRegExpRunner } from "../../regExpRunners";
import { MINUTES, SECONDS } from "../../utils"; import { MINUTES, SECONDS } from "../../utils";
import { registerEventListenersFromMap } from "../../utils/registerEventListenersFromMap"; import { registerEventListenersFromMap } from "../../utils/registerEventListenersFromMap";
import { unregisterEventListenersFromMap } from "../../utils/unregisterEventListenersFromMap"; import { unregisterEventListenersFromMap } from "../../utils/unregisterEventListenersFromMap";
import { StrictValidationError, validate } from "../../validatorUtils"; import { parseIoTsSchema, StrictValidationError } from "../../validatorUtils";
import { CountersPlugin } from "../Counters/CountersPlugin"; import { CountersPlugin } from "../Counters/CountersPlugin";
import { InternalPosterPlugin } from "../InternalPoster/InternalPosterPlugin"; import { InternalPosterPlugin } from "../InternalPoster/InternalPosterPlugin";
import { LogsPlugin } from "../Logs/LogsPlugin"; import { LogsPlugin } from "../Logs/LogsPlugin";
@ -181,12 +180,7 @@ const configParser = (input: unknown) => {
} }
} }
const error = validate(ConfigSchema, input); return parseIoTsSchema(ConfigSchema, input);
if (error) {
throw error;
}
return input as t.TypeOf<typeof ConfigSchema>;
}; };
export const AutomodPlugin = zeppelinGuildPlugin<AutomodPluginType>()({ export const AutomodPlugin = zeppelinGuildPlugin<AutomodPluginType>()({

View file

@ -1,5 +1,4 @@
import { EventEmitter } from "events"; import { EventEmitter } from "events";
import * as t from "io-ts";
import { PluginOptions } from "knub"; import { PluginOptions } from "knub";
import { import {
buildCounterConditionString, buildCounterConditionString,
@ -10,7 +9,7 @@ import {
import { GuildCounters } from "../../data/GuildCounters"; import { GuildCounters } from "../../data/GuildCounters";
import { mapToPublicFn } from "../../pluginUtils"; import { mapToPublicFn } from "../../pluginUtils";
import { convertDelayStringToMS, MINUTES } from "../../utils"; import { convertDelayStringToMS, MINUTES } from "../../utils";
import { StrictValidationError, validate } from "../../validatorUtils"; import { parseIoTsSchema, StrictValidationError } from "../../validatorUtils";
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint"; import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
import { AddCounterCmd } from "./commands/AddCounterCmd"; import { AddCounterCmd } from "./commands/AddCounterCmd";
import { CountersListCmd } from "./commands/CountersListCmd"; import { CountersListCmd } from "./commands/CountersListCmd";
@ -115,12 +114,7 @@ export const CountersPlugin = zeppelinGuildPlugin<CountersPluginType>()({
throw new StrictValidationError([`You can only have at most ${MAX_COUNTERS} counters`]); throw new StrictValidationError([`You can only have at most ${MAX_COUNTERS} counters`]);
} }
const error = validate(ConfigSchema, input); return parseIoTsSchema(ConfigSchema, input);
if (error) {
throw error;
}
return input as t.TypeOf<typeof ConfigSchema>;
}, },
public: { public: {

View file

@ -1,6 +1,5 @@
import * as t from "io-ts";
import { GuildRoleButtons } from "../../data/GuildRoleButtons"; import { GuildRoleButtons } from "../../data/GuildRoleButtons";
import { StrictValidationError, validate } from "../../validatorUtils"; import { parseIoTsSchema, StrictValidationError } from "../../validatorUtils";
import { LogsPlugin } from "../Logs/LogsPlugin"; import { LogsPlugin } from "../Logs/LogsPlugin";
import { RoleManagerPlugin } from "../RoleManager/RoleManagerPlugin"; import { RoleManagerPlugin } from "../RoleManager/RoleManagerPlugin";
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint"; import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
@ -65,12 +64,7 @@ export const RoleButtonsPlugin = zeppelinGuildPlugin<RoleButtonsPluginType>()({
} }
} }
const error = validate(ConfigSchema, input); return parseIoTsSchema(ConfigSchema, input);
if (error) {
throw error;
}
return input as t.TypeOf<typeof ConfigSchema>;
}, },
dependencies: () => [LogsPlugin, RoleManagerPlugin], dependencies: () => [LogsPlugin, RoleManagerPlugin],

View file

@ -1,7 +1,6 @@
import * as t from "io-ts";
import { CooldownManager, PluginOptions } from "knub"; import { CooldownManager, PluginOptions } from "knub";
import { trimPluginDescription } from "../../utils"; import { trimPluginDescription } from "../../utils";
import { validate } from "../../validatorUtils"; import { parseIoTsSchema } from "../../validatorUtils";
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint"; import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
import { RoleAddCmd } from "./commands/RoleAddCmd"; import { RoleAddCmd } from "./commands/RoleAddCmd";
import { RoleHelpCmd } from "./commands/RoleHelpCmd"; import { RoleHelpCmd } from "./commands/RoleHelpCmd";
@ -84,12 +83,7 @@ export const SelfGrantableRolesPlugin = zeppelinGuildPlugin<SelfGrantableRolesPl
} }
} }
const error = validate(ConfigSchema, input); return parseIoTsSchema(ConfigSchema, input);
if (error) {
throw error;
}
return input as t.TypeOf<typeof ConfigSchema>;
}, },
defaultOptions, defaultOptions,

View file

@ -1,10 +1,9 @@
import * as t from "io-ts";
import { PluginOptions } from "knub"; import { PluginOptions } from "knub";
import { GuildSavedMessages } from "../../data/GuildSavedMessages"; import { GuildSavedMessages } from "../../data/GuildSavedMessages";
import { GuildStarboardMessages } from "../../data/GuildStarboardMessages"; import { GuildStarboardMessages } from "../../data/GuildStarboardMessages";
import { GuildStarboardReactions } from "../../data/GuildStarboardReactions"; import { GuildStarboardReactions } from "../../data/GuildStarboardReactions";
import { trimPluginDescription } from "../../utils"; import { trimPluginDescription } from "../../utils";
import { validate } from "../../validatorUtils"; import { parseIoTsSchema } from "../../validatorUtils";
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint"; import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
import { MigratePinsCmd } from "./commands/MigratePinsCmd"; import { MigratePinsCmd } from "./commands/MigratePinsCmd";
import { StarboardReactionAddEvt } from "./events/StarboardReactionAddEvt"; import { StarboardReactionAddEvt } from "./events/StarboardReactionAddEvt";
@ -132,12 +131,7 @@ export const StarboardPlugin = zeppelinGuildPlugin<StarboardPluginType>()({
} }
} }
const error = validate(ConfigSchema, input); return parseIoTsSchema(ConfigSchema, input);
if (error) {
throw error;
}
return input as t.TypeOf<typeof ConfigSchema>;
}, },
defaultOptions, defaultOptions,

View file

@ -1,9 +1,8 @@
import { Snowflake } from "discord.js"; import { Snowflake } from "discord.js";
import humanizeDuration from "humanize-duration"; import humanizeDuration from "humanize-duration";
import * as t from "io-ts";
import { PluginOptions } from "knub"; import { PluginOptions } from "knub";
import moment from "moment-timezone"; import moment from "moment-timezone";
import { StrictValidationError, validate } from "src/validatorUtils"; import { parseIoTsSchema, StrictValidationError } from "src/validatorUtils";
import { GuildArchives } from "../../data/GuildArchives"; import { GuildArchives } from "../../data/GuildArchives";
import { GuildLogs } from "../../data/GuildLogs"; import { GuildLogs } from "../../data/GuildLogs";
import { GuildSavedMessages } from "../../data/GuildSavedMessages"; import { GuildSavedMessages } from "../../data/GuildSavedMessages";
@ -118,12 +117,7 @@ export const TagsPlugin = zeppelinGuildPlugin<TagsPluginType>()({
} }
} }
const error = validate(ConfigSchema, input); return parseIoTsSchema(ConfigSchema, input);
if (error) {
throw error;
}
return input as t.TypeOf<typeof ConfigSchema>;
}, },
beforeLoad(pluginData) { beforeLoad(pluginData) {

View file

@ -1,5 +1,5 @@
import deepDiff from "deep-diff"; import deepDiff from "deep-diff";
import { either, fold } from "fp-ts/lib/Either"; import { either, fold, isLeft } from "fp-ts/lib/Either";
import { pipe } from "fp-ts/lib/pipeable"; import { pipe } from "fp-ts/lib/pipeable";
import * as t from "io-ts"; import * as t from "io-ts";
import { noop } from "./utils"; import { noop } from "./utils";
@ -106,6 +106,14 @@ export function validate(schema: t.Type<any>, value: any): StrictValidationError
); );
} }
export function parseIoTsSchema<T extends t.Type<any>>(schema: T, value: unknown): t.TypeOf<T> {
const decodeResult = schema.decode(value);
if (isLeft(decodeResult)) {
throw report(decodeResult);
}
return decodeResult.right;
}
/** /**
* Decodes and validates the given value against the given schema while also disallowing extra properties * Decodes and validates the given value against the given schema while also disallowing extra properties
* See: https://github.com/gcanti/io-ts/issues/322 * See: https://github.com/gcanti/io-ts/issues/322