Skip to content

Commit

Permalink
fix: add validation to check for duplicate roles
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-ae committed Aug 29, 2024
1 parent a13b2ea commit d62cfe5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/types/plugin-input.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SupportedEvents, SupportedEventsU } from "./context";
import { StaticDecode, Type as T } from "@sinclair/typebox";
import { Static, Type as T } from "@sinclair/typebox";
import { StandardValidator } from "typebox-validators";
import { validateSchemaForDuplicateRoles } from "../utils/validate-schema-for-duplicate-roles";

export interface PluginInputs<T extends SupportedEventsU = SupportedEventsU, TU extends SupportedEvents[T] = SupportedEvents[T]> {
stateId: string;
Expand Down
37 changes: 37 additions & 0 deletions src/utils/validate-schema-for-duplicate-roles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Static, TSchema } from "@sinclair/typebox";
import { startStopSchema } from "../types";

class DuplicateRoleError extends Error {
constructor(message: string) {
super(message);
this.name = "DuplicateRoleError";
}
}

export function validateSchemaForDuplicateRoles<T extends TSchema>(schema: T): T {
return {
...schema,
decode(value: unknown) {
try {
const decodedValue = value as Static<typeof startStopSchema>;

const taskRoles = decodedValue.miscellaneous.maxConcurrentTasks.map((task) => task.role);
const uniqueRoles = new Set(taskRoles);

if (taskRoles.length !== uniqueRoles.size) {
throw new DuplicateRoleError("Duplicate roles found in maxConcurrentTasks.");
}

return decodedValue;
} catch (error) {
if (error instanceof DuplicateRoleError) {
console.error(error.message);
throw error;
} else {
console.error("An unexpected error occurred during decoding:", error);
throw error;
}
}
},
} as T;
}

0 comments on commit d62cfe5

Please sign in to comment.