-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: added docs on how to extend a validator #4489
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
710bb96
docs: added docs on how to extend a validator
shahednasser fcb0dca
Merge branch 'develop' into docs/extend-validator
shahednasser a18f858
Merge branch 'develop' into docs/extend-validator
shahednasser 929d430
Merge branch 'develop' into docs/extend-validator
shahednasser 014be1d
fix eslint error
shahednasser 857347f
Merge branch 'develop' into docs/extend-validator
shahednasser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
--- | ||
description: 'Learn how to extend a validator. This is useful when you want to pass additional data to endpoints in the Medusa core.' | ||
addHowToData: true | ||
--- | ||
|
||
# How to Extend an Endpoint Validator | ||
|
||
In this guide, you'll learn how to extend an endpoint validator from the Medusa core. | ||
|
||
## Overview | ||
|
||
Request fields passed to endpoints that are defined in the Medusa core are validated to ensure that only expected fields are passed, and the passed fields are of correct types. | ||
|
||
In some scenarios, you may need to allow passing custom fields into an existing endpoint. If a custom field is passed to an endpoint in the core, the endpoint returns an error in the response. | ||
|
||
To allow passing custom fields into core endpoints, you must extend Validators. Validators are classes that are used by the core to validate the request parameters to an endpoint. | ||
|
||
This guide explains how to extend a validator to allow passing custom fields to an endpoint. You'll be extending the validator of the admin API Create Product endpoint as an example. | ||
|
||
--- | ||
|
||
## Prerequisites | ||
|
||
This guide assumes you already have a Medusa backend installed and configured. If not, you can check out the [backend quickstart guide](../backend/install.mdx). | ||
|
||
--- | ||
|
||
## Step 1: Create File | ||
|
||
You can add the code to extend the validator in any file under the `src` directory of your Medusa project, but it should be executed by `src/api/index.ts`. | ||
|
||
For example, you can add the code in an exported function defined in the file `src/api/routes/admin/products/create-product.ts`, then import that file in `src/api/index.ts` and execute the function. | ||
|
||
For simplicity, this guide adds the code directly in `src/api/index.ts`. Make sure to create it if it's not already created. | ||
|
||
--- | ||
|
||
## Step 2: Extend Validator | ||
|
||
In the file you created, which in this case is `src/api/index.ts`, add the following content to extend the validator: | ||
|
||
```ts title=src/api/index.ts | ||
import { registerOverriddenValidators } from "@medusajs/medusa" | ||
import { AdminPostProductsReq as MedusaAdminPostProductsReq } from "@medusajs/medusa/dist/api/routes/admin/products/create-product" | ||
import { IsString } from "class-validator" | ||
|
||
class AdminPostProductsReq extends MedusaAdminPostProductsReq { | ||
@IsString() | ||
custom_field: string | ||
} | ||
|
||
registerOverriddenValidators(AdminPostProductsReq) | ||
``` | ||
|
||
In this code snippet you: | ||
|
||
1. Import the `registerOverriddenValidators` function from the `@medusajs/medusa` package. This utility function allows you to extend validators in the core. | ||
2. Import the `AdminPostProductsReq` class from `@medusajs/medusa` as `MedusaAdminPostProductsReq` since this guide extends the Create Product endpoint validator. If you're extending a different validator, make sure to import it instead. | ||
3. Create a class `AdminPostProductsReq` that extends `MedusaAdminPostProductsReq` and adds a new field `custom_field`. Notice that the name of the class must be the same name of the validator defined in the core. `custom_field` has the type `string`. You can change the type or name of the field, or add more fields. | ||
4. Call `registerOverriddenValidators` passing it the `AdminPostProductsReq` class you created. This will override the validator defined in the core to include the new field `custom_field` among the existing fields defined in the core. | ||
|
||
:::tip | ||
|
||
Validators are defined in the same file as the endpoint. To find the validator you need to override, find the endpoint file under `@medusajs/medusa/dist/api/routes` and import the validator in that file. | ||
|
||
::: | ||
|
||
--- | ||
|
||
## Step 3: Test it Out | ||
|
||
To test out your extended validator, build and start your Medusa backend: | ||
|
||
```bash npm2yarn | ||
npm run build | ||
npx @medusajs/medusa-cli develop | ||
``` | ||
|
||
Then, send a request to the endpoint you extended passing it your custom fields. To test out the example in this guide, send an [authenticated request](/api/admin#section/Authentication) to the [Create Product endpoint](https://docs.medusajs.com/api/admin#tag/Products/operation/PostProducts) and pass it the `custom_field` body parameter. The request should execute with no errors. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: would be nice to add some reference for this at some point, but this will do for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would hold off this change for now as we are changing that to mutualise it with the fields and relations override in @riqwan pr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah got it, I'll close this for now then until that PR is merged. cc @olivermrbl