Skip to content

Commit d9946e6

Browse files
authored
docs: add section on validating module options (medusajs#11427)
1 parent 00fa475 commit d9946e6

File tree

3 files changed

+14102
-14012
lines changed

3 files changed

+14102
-14012
lines changed

www/apps/book/app/learn/fundamentals/modules/options/page.mdx

+45
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,48 @@ export default async function helloWorldLoader({
118118
)
119119
}
120120
```
121+
122+
---
123+
124+
## Validate Module Options
125+
126+
If you expect a certain option and want to throw an error if it's not provided or isn't valid, it's recommended to perform the validation in a loader. The module's service is only instantiated when it's used, whereas the loader runs the when the Medusa application starts.
127+
128+
So, by performing the validation in the loader, you ensure you can throw an error at an early point, rather than when the module is used.
129+
130+
For example, to validate that the Hello Module received an `apiKey` option, create the loader `src/modules/loaders/validate.ts`:
131+
132+
```ts title="src/modules/hello/loaders/validate.ts"
133+
import { LoaderOptions } from "@medusajs/framework/types"
134+
import { MedusaError } from "@medusajs/framework/utils"
135+
136+
// recommended to define type in another file
137+
type ModuleOptions = {
138+
apiKey?: string
139+
}
140+
141+
export default async function validationLoader({
142+
options,
143+
}: LoaderOptions<ModuleOptions>) {
144+
if (!options.apiKey) {
145+
throw new MedusaError(
146+
MedusaError.Types.INVALID_DATA,
147+
"Hello Module requires an apiKey option."
148+
)
149+
}
150+
}
151+
```
152+
153+
Then, export the loader in the module's definition file, as explained in [this chapter](../loaders/page.mdx):
154+
155+
```ts title="src/modules/hello/index.ts"
156+
// other imports...
157+
import validationLoader from "./loaders/validate"
158+
159+
export default Module("hello", {
160+
// ...
161+
loaders: [validationLoader],
162+
})
163+
```
164+
165+
Now, when the Medusa application starts, the loader will run, validating the module's options and throwing an error if the `apiKey` option is missing.

www/apps/book/generated/edit-dates.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const generatedEditDates = {
4040
"app/learn/fundamentals/data-models/write-migration/page.mdx": "2024-11-11T15:27:59.794Z",
4141
"app/learn/fundamentals/data-models/manage-relationships/page.mdx": "2025-02-11T15:53:12.541Z",
4242
"app/learn/fundamentals/modules/remote-query/page.mdx": "2024-07-21T21:20:24+02:00",
43-
"app/learn/fundamentals/modules/options/page.mdx": "2025-01-16T09:21:38.244Z",
43+
"app/learn/fundamentals/modules/options/page.mdx": "2025-02-12T16:00:28.484Z",
4444
"app/learn/fundamentals/data-models/relationships/page.mdx": "2025-02-03T08:01:18.094Z",
4545
"app/learn/fundamentals/workflows/compensation-function/page.mdx": "2024-12-06T14:34:50.384Z",
4646
"app/learn/fundamentals/modules/service-factory/page.mdx": "2024-10-21T13:30:21.371Z",

0 commit comments

Comments
 (0)