Rank change system UI first pass
This commit is contained in:
29
shared/schemas/promotionSchema.ts
Normal file
29
shared/schemas/promotionSchema.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const batchPromotionMemberSchema = z.object({
|
||||
member_id: z.number({ invalid_type_error: "Must select a member" }).int().positive(),
|
||||
rank_id: z.number({ invalid_type_error: "Must select a rank" }).int().positive(),
|
||||
start_date: z.string().refine((val) => !isNaN(Date.parse(val)), {
|
||||
message: "Must be a valid date",
|
||||
}),
|
||||
reason: z.string({ required_error: "Reason is required" }).max(50, "Reason too long"),
|
||||
});
|
||||
|
||||
export const batchPromotionSchema = z.object({
|
||||
promotions: z.array(batchPromotionMemberSchema).nonempty({ message: "At least one promotion is required" }),
|
||||
|
||||
})
|
||||
.superRefine((data, ctx) => {
|
||||
// optional: check for duplicate member_ids
|
||||
const memberCounts = new Map<number, number>();
|
||||
data.promotions.forEach((p, index) => {
|
||||
memberCounts.set(p.member_id, (memberCounts.get(p.member_id) ?? 0) + 1);
|
||||
if (memberCounts.get(p.member_id)! > 1) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
path: ["promotions", index, "member_id"],
|
||||
message: "Duplicate member in batch is not allowed",
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user