refactor: replace io-ts with zod

This commit is contained in:
Dragory 2024-01-14 14:25:42 +00:00
parent fafaefa1fb
commit 28692962bc
No known key found for this signature in database
161 changed files with 1450 additions and 2105 deletions

View file

@ -1,11 +1,10 @@
import { CooldownManager, PluginOptions } from "knub";
import { trimPluginDescription } from "../../utils";
import { parseIoTsSchema } from "../../validatorUtils";
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
import { RoleAddCmd } from "./commands/RoleAddCmd";
import { RoleHelpCmd } from "./commands/RoleHelpCmd";
import { RoleRemoveCmd } from "./commands/RoleRemoveCmd";
import { ConfigSchema, SelfGrantableRolesPluginType, defaultSelfGrantableRoleEntry } from "./types";
import { SelfGrantableRolesPluginType, zSelfGrantableRolesConfig } from "./types";
const defaultOptions: PluginOptions<SelfGrantableRolesPluginType> = {
config: {
@ -66,25 +65,10 @@ export const SelfGrantableRolesPlugin = zeppelinGuildPlugin<SelfGrantableRolesPl
can_use: true
~~~
`),
configSchema: ConfigSchema,
configSchema: zSelfGrantableRolesConfig,
},
configParser: (input) => {
const entries = (input as any).entries;
for (const [key, entry] of Object.entries<any>(entries)) {
// Apply default entry config
entries[key] = { ...defaultSelfGrantableRoleEntry, ...entry };
// Normalize alias names
if (entry.roles) {
for (const [roleId, aliases] of Object.entries<string[]>(entry.roles)) {
entry.roles[roleId] = aliases.map((a) => a.toLowerCase());
}
}
}
return parseIoTsSchema(ConfigSchema, input);
},
configParser: (input) => zSelfGrantableRolesConfig.parse(input),
defaultOptions,
// prettier-ignore

View file

@ -1,31 +1,29 @@
import * as t from "io-ts";
import { BasePluginType, CooldownManager, guildPluginMessageCommand } from "knub";
import z from "zod";
import { zBoundedCharacters, zBoundedRecord } from "../../utils";
const RoleMap = t.record(t.string, t.array(t.string));
const zRoleMap = z.record(
zBoundedCharacters(1, 100),
z.array(zBoundedCharacters(1, 2000))
.max(100)
.transform((parsed) => parsed.map(v => v.toLowerCase())),
);
const SelfGrantableRoleEntry = t.type({
roles: RoleMap,
can_use: t.boolean,
can_ignore_cooldown: t.boolean,
max_roles: t.number,
const zSelfGrantableRoleEntry = z.strictObject({
roles: zBoundedRecord(zRoleMap, 0, 100),
can_use: z.boolean().default(false),
can_ignore_cooldown: z.boolean().default(false),
max_roles: z.number().default(0),
});
const PartialRoleEntry = t.partial(SelfGrantableRoleEntry.props);
export type TSelfGrantableRoleEntry = t.TypeOf<typeof SelfGrantableRoleEntry>;
export type TSelfGrantableRoleEntry = z.infer<typeof zSelfGrantableRoleEntry>;
export const ConfigSchema = t.type({
entries: t.record(t.string, SelfGrantableRoleEntry),
mention_roles: t.boolean,
export const zSelfGrantableRolesConfig = z.strictObject({
entries: zBoundedRecord(z.record(zBoundedCharacters(0, 255), zSelfGrantableRoleEntry), 0, 100),
mention_roles: z.boolean(),
});
type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
export const defaultSelfGrantableRoleEntry: t.TypeOf<typeof PartialRoleEntry> = {
can_use: false,
can_ignore_cooldown: false,
max_roles: 0,
};
export interface SelfGrantableRolesPluginType extends BasePluginType {
config: TConfigSchema;
config: z.infer<typeof zSelfGrantableRolesConfig>;
state: {
cooldowns: CooldownManager;
};