From 710bb960831284e6f90895265372e02f8cdf8415 Mon Sep 17 00:00:00 2001 From: Shahed nasser Date: Mon, 10 Jul 2023 17:17:10 +0300 Subject: [PATCH 1/2] docs: added docs on how to extend a validator --- .../development/endpoints/extend-validator.md | 79 +++++++++++++++++++ .../development/entities/extend-entity.md | 8 ++ www/docs/sidebars.js | 5 ++ 3 files changed, 92 insertions(+) create mode 100644 docs/content/development/endpoints/extend-validator.md diff --git a/docs/content/development/endpoints/extend-validator.md b/docs/content/development/endpoints/extend-validator.md new file mode 100644 index 0000000000000..905d275ec9874 --- /dev/null +++ b/docs/content/development/endpoints/extend-validator.md @@ -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. diff --git a/docs/content/development/entities/extend-entity.md b/docs/content/development/entities/extend-entity.md index 06c4472f7669e..7a761d5d26c1f 100644 --- a/docs/content/development/entities/extend-entity.md +++ b/docs/content/development/entities/extend-entity.md @@ -161,6 +161,14 @@ You can now use your extended entity and its repository throughout your commerce ## Access Custom Attributes and Relations in Core Endpoints +### Request Parameters + +In most cases, after you extend an entity to add new attributes, you'll likely need to pass these attributes to endpoints defined in the core. By default, this causes an error, as request parameters are validated to ensure only those that are defined are passed to the endpoint. + +To allow passing your custom attribute, you'll need to [extend the validator](../endpoints/extend-validator.md) of the endpoint. + +### Response Fields + After you add custom attributes, you'll notice that these attributes aren't returned as part of the response fields of core endpoints. Core endpoints have a defined set of fields and relations that can be returned by default in requests. To change that and ensure your custom attribute is returned in your request, you can extend the allowed fields of a set of endpoints in a loader file and add your attribute into them. diff --git a/www/docs/sidebars.js b/www/docs/sidebars.js index d3998cd021cde..117d3171502e4 100644 --- a/www/docs/sidebars.js +++ b/www/docs/sidebars.js @@ -1378,6 +1378,11 @@ module.exports = { id: "development/endpoints/add-middleware", label: "Middleware", }, + { + type: "doc", + id: "development/endpoints/extend-validator", + label: "Extend Validator", + }, { type: "doc", id: "development/endpoints/example-logged-in-user", From 014be1dad7509ce4f08591b39f479ced667e4e77 Mon Sep 17 00:00:00 2001 From: Shahed nasser Date: Tue, 18 Jul 2023 17:40:35 +0300 Subject: [PATCH 2/2] fix eslint error --- docs/content/development/endpoints/extend-validator.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/content/development/endpoints/extend-validator.md b/docs/content/development/endpoints/extend-validator.md index 905d275ec9874..c31822ad592da 100644 --- a/docs/content/development/endpoints/extend-validator.md +++ b/docs/content/development/endpoints/extend-validator.md @@ -39,9 +39,13 @@ For simplicity, this guide adds the code directly in `src/api/index.ts`. Make su 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 { + AdminPostProductsReq as MedusaAdminPostProductsReq, +} from "@medusajs/medusa/dist/api/routes/admin/products/create-product" import { IsString } from "class-validator" class AdminPostProductsReq extends MedusaAdminPostProductsReq {