libretto 0.6.18 → 0.6.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts CHANGED
@@ -100,11 +100,13 @@ export {
100
100
  LibrettoWorkflow,
101
101
  LibrettoWorkflowInputError,
102
102
  LIBRETTO_WORKFLOW_BRAND,
103
+ validateWorkflowInput,
103
104
  workflow,
104
105
  type ExportedLibrettoWorkflow,
105
106
  type LibrettoWorkflowContext,
106
107
  type LibrettoWorkflowHandler,
107
108
  type LibrettoWorkflowSchemas,
109
+ type WorkflowInputValidator,
108
110
  } from "./shared/workflow/workflow.js";
109
111
  const isDirectExecution = (): boolean => {
110
112
  const entryArg = process.argv[1];
@@ -49,6 +49,32 @@ function formatZodErrorMessage(
49
49
  ].join("\n");
50
50
  }
51
51
 
52
+ function parseWorkflowInput<InputSchema extends z.ZodType>(
53
+ workflowName: string,
54
+ inputSchema: InputSchema | undefined,
55
+ input: unknown,
56
+ ): z.infer<InputSchema> {
57
+ if (!inputSchema) return input as z.infer<InputSchema>;
58
+
59
+ const result = inputSchema.safeParse(input);
60
+ if (!result.success) {
61
+ throw new LibrettoWorkflowInputError(workflowName, result.error);
62
+ }
63
+ return result.data;
64
+ }
65
+
66
+ export type WorkflowInputValidator = {
67
+ readonly name: string;
68
+ readonly inputSchema?: z.ZodType;
69
+ };
70
+
71
+ export function validateWorkflowInput(
72
+ workflow: WorkflowInputValidator,
73
+ input: unknown,
74
+ ): void {
75
+ parseWorkflowInput(workflow.name, workflow.inputSchema, input);
76
+ }
77
+
52
78
  export class LibrettoWorkflow<
53
79
  InputSchema extends z.ZodType = z.ZodType<unknown>,
54
80
  OutputSchema extends z.ZodType = z.ZodType<unknown>,
@@ -86,16 +112,7 @@ export class LibrettoWorkflow<
86
112
  ctx: LibrettoWorkflowContext,
87
113
  input: unknown,
88
114
  ): Promise<z.infer<OutputSchema>> {
89
- let parsed: z.infer<InputSchema>;
90
- if (this.inputSchema) {
91
- const result = this.inputSchema.safeParse(input);
92
- if (!result.success) {
93
- throw new LibrettoWorkflowInputError(this.name, result.error);
94
- }
95
- parsed = result.data;
96
- } else {
97
- parsed = input as z.infer<InputSchema>;
98
- }
115
+ const parsed = parseWorkflowInput(this.name, this.inputSchema, input);
99
116
  return this.handler(ctx, parsed);
100
117
  }
101
118
  }