All files / src/entities/labels schema.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 0/0
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 722x 2x                   2x 2x 2x 2x               2x                           2x                                 2x             2x                      
import { z } from "zod";
import { requiredId } from "../utils";
 
// ============================================================================
// manage_label - CQRS Command Tool (discriminated union schema)
// Actions: create, update, delete
// Uses z.discriminatedUnion() for type-safe action handling.
// Schema pipeline flattens to flat JSON Schema for AI clients that don't support oneOf.
// ============================================================================
 
// --- Shared fields ---
const namespaceField = z.string().describe("Namespace path (group or project)");
const labelIdField = requiredId.describe("The ID or title of the label");
const descriptionField = z.string().optional().describe("The description of the label");
const priorityField = z
  .number()
  .optional()
  .describe(
    "The priority of the label. Must be greater or equal than zero or null to remove the priority."
  );
 
// --- Action: create ---
const CreateLabelSchema = z.object({
  action: z.literal("create").describe("Create a new label"),
  namespace: namespaceField,
  name: z.string().describe("The name of the label"),
  color: z
    .string()
    .describe(
      "The color of the label in 6-digit hex notation with leading '#' (e.g. #FFAABB) or CSS color name"
    ),
  description: descriptionField,
  priority: priorityField,
});
 
// --- Action: update ---
const UpdateLabelSchema = z.object({
  action: z.literal("update").describe("Update an existing label"),
  namespace: namespaceField,
  label_id: labelIdField,
  name: z.string().optional().describe("The name of the label"),
  new_name: z.string().optional().describe("The new name of the label"),
  color: z
    .string()
    .optional()
    .describe(
      "The color of the label in 6-digit hex notation with leading '#' (e.g. #FFAABB) or CSS color name"
    ),
  description: descriptionField,
  priority: priorityField,
});
 
// --- Action: delete ---
const DeleteLabelSchema = z.object({
  action: z.literal("delete").describe("Delete a label"),
  namespace: namespaceField,
  label_id: labelIdField,
});
 
// --- Discriminated union combining all actions ---
export const ManageLabelSchema = z.discriminatedUnion("action", [
  CreateLabelSchema,
  UpdateLabelSchema,
  DeleteLabelSchema,
]);
 
// ============================================================================
// Type exports
// ============================================================================
 
export type ManageLabelInput = z.infer<typeof ManageLabelSchema>;