1 ---
2 sidebar_label: "no-throw-in-transform"
3 ---
4
5 export const metadata = {
6 title: `no-throw-in-transform - ESLint plugin rules`,
7 }
8
9 # {metadata.title}
10
11 This rule disallows `throw` statements inside a `transform()` callback.
12
13 ## Severity
14
15 `error`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset.
16
17 ## What it Targets
18
19 This rule targets files in your project's `src/workflows` directory. It reports `throw` statements placed directly inside a `transform()` callback.
20
21 The following code is reported by the rule:
22
23 ```ts title="src/workflows/example.ts"
24 import {
25 createWorkflow,
26 transform,
27 WorkflowResponse,
28 } from "@medusajs/framework/workflows-sdk"
29
30 createWorkflow("my-workflow", (input) => {
31 const value = transform({ input }, (data) => {
32 if (!data.input.foo) {
33 throw new Error("invalid")
34 }
35 return data.input.foo
36 })
37
38 return new WorkflowResponse(value)
39 })
40 ```
41
42 Instead, use a validation step or `when-then` to gate the work:
43
44 ```ts title="src/workflows/example.ts"
45 import {
46 createWorkflow,
47 createStep,
48 WorkflowResponse,
49 } from "@medusajs/framework/workflows-sdk"
50
51 const validateStep = createStep("validate", (input) => {
52 if (!input.foo) {
53 throw new Error("invalid")
54 }
55 })
56
57 createWorkflow("my-workflow", (input) => {
58 validateStep(input)
59
60 return new WorkflowResponse(input)
61 })
62 ```
63
64 ## Why it's Important
65
66 `transform` is only meant for data transformation. Use a validation step or `when-then` to gate the work instead.
67
68 Learn more in the [Workflows documentation](!docs!/learn/fundamentals/workflows).
69
70 ## Fixable
71
72 This rule isn't auto-fixable.