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 { errorMessage, successMessage, tNullable } from "./utils";
import { Tail } from "./utils/typeUtils";
import { StrictValidationError, validate } from "./validatorUtils";
import { parseIoTsSchema, StrictValidationError } from "./validatorUtils";
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> {
return (input: unknown) => {
const error = validate(schema, input);
if (error) {
throw strictValidationErrorToConfigValidationError(error);
try {
return parseIoTsSchema(schema, input);
} 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 { GuildAntiraidLevels } from "../../data/GuildAntiraidLevels";
import { GuildArchives } from "../../data/GuildArchives";
@ -9,7 +8,7 @@ import { discardRegExpRunner, getRegExpRunner } from "../../regExpRunners";
import { MINUTES, SECONDS } from "../../utils";
import { registerEventListenersFromMap } from "../../utils/registerEventListenersFromMap";
import { unregisterEventListenersFromMap } from "../../utils/unregisterEventListenersFromMap";
import { StrictValidationError, validate } from "../../validatorUtils";
import { parseIoTsSchema, StrictValidationError } from "../../validatorUtils";
import { CountersPlugin } from "../Counters/CountersPlugin";
import { InternalPosterPlugin } from "../InternalPoster/InternalPosterPlugin";
import { LogsPlugin } from "../Logs/LogsPlugin";
@ -181,12 +180,7 @@ const configParser = (input: unknown) => {
}
}
const error = validate(ConfigSchema, input);
if (error) {
throw error;
}
return input as t.TypeOf<typeof ConfigSchema>;
return parseIoTsSchema(ConfigSchema, input);
};
export const AutomodPlugin = zeppelinGuildPlugin<AutomodPluginType>()({

View file

@ -1,5 +1,4 @@
import { EventEmitter } from "events";
import * as t from "io-ts";
import { PluginOptions } from "knub";
import {
buildCounterConditionString,
@ -10,7 +9,7 @@ import {
import { GuildCounters } from "../../data/GuildCounters";
import { mapToPublicFn } from "../../pluginUtils";
import { convertDelayStringToMS, MINUTES } from "../../utils";
import { StrictValidationError, validate } from "../../validatorUtils";
import { parseIoTsSchema, StrictValidationError } from "../../validatorUtils";
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
import { AddCounterCmd } from "./commands/AddCounterCmd";
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`]);
}
const error = validate(ConfigSchema, input);
if (error) {
throw error;
}
return input as t.TypeOf<typeof ConfigSchema>;
return parseIoTsSchema(ConfigSchema, input);
},
public: {

View file

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

View file

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

View file

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

View file

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

View file

@ -1,5 +1,5 @@
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 * as t from "io-ts";
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
* See: https://github.com/gcanti/io-ts/issues/322