refactor: replace io-ts with zod
This commit is contained in:
parent
fafaefa1fb
commit
28692962bc
161 changed files with 1450 additions and 2105 deletions
|
@ -10,22 +10,18 @@ import {
|
|||
PermissionsBitField,
|
||||
TextBasedChannel,
|
||||
} from "discord.js";
|
||||
import * as t from "io-ts";
|
||||
import {
|
||||
AnyPluginData,
|
||||
CommandContext,
|
||||
ConfigValidationError,
|
||||
ExtendedMatchParams,
|
||||
GuildPluginData,
|
||||
PluginOverrideCriteria,
|
||||
helpers,
|
||||
helpers
|
||||
} from "knub";
|
||||
import { logger } from "./logger";
|
||||
import { isStaff } from "./staff";
|
||||
import { TZeppelinKnub } from "./types";
|
||||
import { errorMessage, successMessage, tNullable } from "./utils";
|
||||
import { errorMessage, successMessage } from "./utils";
|
||||
import { Tail } from "./utils/typeUtils";
|
||||
import { StrictValidationError, parseIoTsSchema } from "./validatorUtils";
|
||||
|
||||
const { getMemberLevel } = helpers;
|
||||
|
||||
|
@ -59,46 +55,6 @@ export async function hasPermission(
|
|||
return helpers.hasPermission(config, permission);
|
||||
}
|
||||
|
||||
const PluginOverrideCriteriaType: t.Type<PluginOverrideCriteria<unknown>> = t.recursion(
|
||||
"PluginOverrideCriteriaType",
|
||||
() =>
|
||||
t.partial({
|
||||
channel: tNullable(t.union([t.string, t.array(t.string)])),
|
||||
category: tNullable(t.union([t.string, t.array(t.string)])),
|
||||
level: tNullable(t.union([t.string, t.array(t.string)])),
|
||||
user: tNullable(t.union([t.string, t.array(t.string)])),
|
||||
role: tNullable(t.union([t.string, t.array(t.string)])),
|
||||
|
||||
all: tNullable(t.array(PluginOverrideCriteriaType)),
|
||||
any: tNullable(t.array(PluginOverrideCriteriaType)),
|
||||
not: tNullable(PluginOverrideCriteriaType),
|
||||
|
||||
extra: t.unknown,
|
||||
}),
|
||||
);
|
||||
|
||||
export function strictValidationErrorToConfigValidationError(err: StrictValidationError) {
|
||||
return new ConfigValidationError(
|
||||
err
|
||||
.getErrors()
|
||||
.map((e) => e.toString())
|
||||
.join("\n"),
|
||||
);
|
||||
}
|
||||
|
||||
export function makeIoTsConfigParser<Schema extends t.Type<any>>(schema: Schema): (input: unknown) => t.TypeOf<Schema> {
|
||||
return (input: unknown) => {
|
||||
try {
|
||||
return parseIoTsSchema(schema, input);
|
||||
} catch (err) {
|
||||
if (err instanceof StrictValidationError) {
|
||||
throw strictValidationErrorToConfigValidationError(err);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export async function sendSuccessMessage(
|
||||
pluginData: AnyPluginData<any>,
|
||||
channel: TextBasedChannel,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue