Skip to content

Commit

Permalink
feat: Guild, Role, RoleGroup schemas (#1567)
Browse files Browse the repository at this point in the history
* feat: Guild, Role, RoleGroup schemas

* fix: use `.nullish()`

* fix: use `.nonnegative()` for memberCount and roleCount

* fix: change `groupId`'s type to UUID
  • Loading branch information
BrickheadJohnny authored Nov 26, 2024
1 parent c4333d6 commit d6bab0a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/lib/schemas/common.ts
Original file line number Diff line number Diff line change
@@ -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());
13 changes: 8 additions & 5 deletions src/lib/schemas/guild.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
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(),
});

export type CreateGuildForm = z.infer<typeof CreateGuildSchema>;

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<typeof GuildSchema>;
30 changes: 30 additions & 0 deletions src/lib/schemas/role.ts
Original file line number Diff line number Diff line change
@@ -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<typeof CreateRoleSchema>;

const RoleSchema = CreateRoleSchema.extend({
id: z.string().uuid(),
createdAt: DateLike,
updatedAt: DateLike,
memberCount: z.number().nonnegative(),
});

export type Role = z.infer<typeof RoleSchema>;
18 changes: 18 additions & 0 deletions src/lib/schemas/roleGroup.ts
Original file line number Diff line number Diff line change
@@ -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<typeof CreateRoleGroupSchema>;

const RoleGroupSchema = CreateRoleGroupSchema.extend({
id: z.string().uuid(),
createdAt: DateLike,
updatedAt: DateLike,
});

export type RoleGroup = z.infer<typeof RoleGroupSchema>;

0 comments on commit d6bab0a

Please sign in to comment.