#How to add validator using medusa-extender for API request in medusa-react with additional parameter

29 messages · Page 1 of 1 (latest)

pulsar halo
#

Hi, I have added two new columns in product_variant table. I have successfully added validator using medusa-extender for these two new properties in the useAdminCreateVariant method in medusa-react. But I cant figure out how to do to it for useAdminUpdateVariant since it also passes an additional variant_id parameter? The method definition looks like this

export const useAdminUpdateVariant = (
  ...
  options?: UseMutationOptions<
    ...
    AdminPostProductsProductVariantsReq & { variant_id: string }
  >
) => { ... }

How do I target AdminPostProductsProductVariantsReq & { variant_id: string } in the validator?

#

@random meteor

random meteor
#

Have you look at the custom validator part of the doc? Is the problem in react or in the backend ?

pulsar halo
#

@random meteor Yes, I have extended the validator for the CreateVariant function using medusa-extender like this:

@Validator({ override: AdminPostProductsProductVariantsReq })
export class ExtendedCreateProductVariantValidator extends AdminPostProductsProductVariantsReq {
  @IsString()
  gtin: string;

  @IsString()
  mpn: string;
}

But this doesn't work for updateVariant since updateVariant in medusa-react calls the same function but has an additional arg variant_id declared like this AdminPostProductsProductVariantsReq & { variant_id: string }. And I can't figure out how to extend the validator when there are additional args. Can't seem to find any example in medusa-extender docs either

random meteor
#

Why don’t you extend the update variant validator? You have only extended the create and you can’t expect the update to know about it.

#

Here is the update variant validator AdminPostProductsProductVariantsVariantReq

#

Is the & { variant_id } in the react package? It seems to be weird

#

Since the variant id is a param of the request

pulsar halo
#

Yeah sorry my bad. I was looking at the medusa-react source code here: https://github.com/medusajs/medusa/blob/master/packages/medusa-react/src/hooks/admin/products/mutations.ts
and it had the same validator defined for createVariant and updateVariant which was confusing me. I have extended AdminPostProductsProductVariantsVariantReq in my medusa server and it works now. Thanks!

GitHub

The open-source Shopify alternative ⚡️. Contribute to medusajs/medusa development by creating an account on GitHub.

pulsar halo
random meteor
#

My pleasure, it might be worth opening an issue so that we can double check 😊

pulsar halo
#

Yeah sure ill do so

obtuse yew
#

Hey did anything change about this? I am unable to achieve the same thing and I keep getting "property series should not exist".

And I have the above mentioned validators and loaded them in my modules

random meteor
#

Can you share what you ve done please

obtuse yew
#

Thank for your halp Adrien

#

Medusa logs also say:
[Server] - 2/24/2023, 8:19:51 AM [ValidatorsLoader] 4 validators applied

Which is pretty accurate. I assume I must've messed up somewhere else then..

random meteor
#

And what version of medusa and the extender do you have? So that we have all the elements. Normally nothing has changed around the validator feature in any of the packages but just in case.

obtuse yew
#

├─ @medusajs/medusa-cli@1.3.5
└─ @medusajs/medusa@1.7.0

└─ medusa-extender@1.7.6
└─ medusa-interfaces@1.3.3

obtuse yew
#

Both node16 and 17 behave the same so it's not node version related either.

obtuse yew
#

Upgrading to the last medusa-core didn't help either.
Actually it messed up a little.

obtuse yew
#

Funny enough, upgrading to the last version broke my Product customizations, it is now unable to read custom fields and relationships and of course logs will tell me the module was loaded correctly 😭

random meteor
#

You mean 1.7.7? And extender 1.8.8?

#

It is still weird, eventually i would understand with the next version since it is now using typeorm 3 but otherwise I don’t see why

#

Some companies are using the extender with the latest medusa with no issues

obtuse yew
#

1.7.8, I guess this is the reason. Oddly enough the other extensions work fine

random meteor
#

I don’t have time to dig at the moment as i am on important stuff on medusa core but asap i ll look at it. If you find something let me know

obtuse yew
#

I was able to pinpoint the issue but did not find a solution. The issue is introduced with v1.7.6 and it does not like my ProductService.

It seems to argue with the constructor and this might be the reason it misbehaves.

I have yet to figure out what makes validators act up