From d6bab0adf7c944a5ab5b53ff9153cfff5c2555d1 Mon Sep 17 00:00:00 2001 From: BrickheadJohnny <92519134+BrickheadJohnny@users.noreply.github.com> Date: Tue, 26 Nov 2024 14:42:33 +0100 Subject: [PATCH] feat: Guild, Role, RoleGroup schemas (#1567) * feat: Guild, Role, RoleGroup schemas * fix: use `.nullish()` * fix: use `.nonnegative()` for memberCount and roleCount * fix: change `groupId`'s type to UUID --- src/lib/schemas/common.ts | 11 +++++++++++ src/lib/schemas/guild.ts | 13 ++++++++----- src/lib/schemas/role.ts | 30 ++++++++++++++++++++++++++++++ src/lib/schemas/roleGroup.ts | 18 ++++++++++++++++++ 4 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 src/lib/schemas/common.ts create mode 100644 src/lib/schemas/role.ts create mode 100644 src/lib/schemas/roleGroup.ts diff --git a/src/lib/schemas/common.ts b/src/lib/schemas/common.ts new file mode 100644 index 0000000000..36ce381d31 --- /dev/null +++ b/src/lib/schemas/common.ts @@ -0,0 +1,11 @@ +import { z } from "zod"; + +export const NameSchema = z + .string() + .max(255, "Maximum name length is 255 characters"); + +export const ImageUrlSchema = z.string().url().max(255); + +export const LogicSchema = z.enum(["AND", "OR", "ANY_OF"]); + +export const DateLike = z.date().or(z.string().datetime()); diff --git a/src/lib/schemas/guild.ts b/src/lib/schemas/guild.ts index 25d526e0ac..9237b333e3 100644 --- a/src/lib/schemas/guild.ts +++ b/src/lib/schemas/guild.ts @@ -1,12 +1,11 @@ import { z } from "zod"; +import { DateLike, ImageUrlSchema, NameSchema } from "./common"; export const CreateGuildSchema = z.object({ - name: z - .string() - .min(1, "You must specify a name for your guild") - .max(255, "Maximum name length is 255 characters"), + name: NameSchema.min(1, "You must specify a name for your guild"), urlName: z.string().max(255).optional(), - imageUrl: z.string().max(255).optional(), + imageUrl: ImageUrlSchema.optional(), + description: z.string().optional(), contact: z.string().email(), }); @@ -14,6 +13,10 @@ export type CreateGuildForm = z.infer; const GuildSchema = CreateGuildSchema.extend({ id: z.string().uuid(), + createdAt: DateLike, + updatedAt: DateLike, + roleCount: z.number().nonnegative(), + memberCount: z.number().nonnegative(), }); export type Guild = z.infer; diff --git a/src/lib/schemas/role.ts b/src/lib/schemas/role.ts new file mode 100644 index 0000000000..a2ccb12209 --- /dev/null +++ b/src/lib/schemas/role.ts @@ -0,0 +1,30 @@ +import { z } from "zod"; +import { DateLike, ImageUrlSchema, LogicSchema, NameSchema } from "./common"; + +export const CreateRoleSchema = z.object({ + name: NameSchema.min(1, "You must specify a name for the role"), + description: z.string().nullish(), + imageUrl: ImageUrlSchema.nullish(), + settings: z + .object({ + logic: LogicSchema, + position: z.number().positive().nullish(), + anyOfNum: z.number().positive().optional(), + }) + .default({ + logic: "AND", + anyOfNum: 1, + }), + groupId: z.string().uuid(), +}); + +export type CreateRoleForm = z.infer; + +const RoleSchema = CreateRoleSchema.extend({ + id: z.string().uuid(), + createdAt: DateLike, + updatedAt: DateLike, + memberCount: z.number().nonnegative(), +}); + +export type Role = z.infer; diff --git a/src/lib/schemas/roleGroup.ts b/src/lib/schemas/roleGroup.ts new file mode 100644 index 0000000000..e94b82be52 --- /dev/null +++ b/src/lib/schemas/roleGroup.ts @@ -0,0 +1,18 @@ +import { z } from "zod"; +import { DateLike, ImageUrlSchema, NameSchema } from "./common"; + +const CreateRoleGroupSchema = z.object({ + name: NameSchema.min(1, "You must specify a name for the role group"), + description: z.string().nullish(), + imageUrl: ImageUrlSchema.nullish(), +}); + +export type CreateRoleGroupForm = z.infer; + +const RoleGroupSchema = CreateRoleGroupSchema.extend({ + id: z.string().uuid(), + createdAt: DateLike, + updatedAt: DateLike, +}); + +export type RoleGroup = z.infer;