Skip to content

adding validation on the ShipmentMethodCode#68

Open
AhmedElmehalawi wants to merge 1 commit intoVirtoCommerce:devfrom
AhmedElmehalawi:dev
Open

adding validation on the ShipmentMethodCode#68
AhmedElmehalawi wants to merge 1 commit intoVirtoCommerce:devfrom
AhmedElmehalawi:dev

Conversation

@AhmedElmehalawi
Copy link

Description

adding validation on the ShipmentMethodCode to be mandatory sent.

Issue

On the AddOrUpdateCartShipment mutaion, if the ShipmentMethodCode is not sent, a new shipment with null ShipmentMethodCode is being created which causes a lot of issues.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@vc-ci
Copy link
Contributor

vc-ci commented Aug 2, 2025

Review task created: https://virtocommerce.atlassian.net/browse/VCST-3723

@ksavosteev
Copy link
Collaborator

Moved to #74. Closing this one.

@ksavosteev ksavosteev closed this Aug 20, 2025
@ksavosteev
Copy link
Collaborator

This is accepted behavior since we could create an empty shipment without a code, for example a shipment with only address while a specific shipment method was not selected by the user. What kind of issues do you have with this behavior?

@ksavosteev ksavosteev reopened this Aug 20, 2025
@AhmedElmehalawi
Copy link
Author

AhmedElmehalawi commented Aug 20, 2025

This is accepted behavior since we could create an empty shipment without a code, for example a shipment with only address while a specific shipment method was not selected by the user. What kind of issues do you have with this behavior?

I don't see it as a valid business case, how you will validate on the price of this unknown shipment? The request can be intercepted and any value can be sent to the backend without any validation on the price amount.

@ksavosteev
Copy link
Collaborator

Creating a shipment object without the code is a valid business case for us. Shipment validator will be invoked before creating an order so in case of invalid shipment price the order will not be created.
If you need to add your own custom validation logic to I'd suggest using Extensibility Framework:

  1. Create a new module with dependency on XCart.

  2. Create a new validator with your custom logic (extending the existing CartShipmentValidator):

    public class CartShipmentValidator2 : CartShipmentValidator
    {
        public CartShipmentValidator2()
        {
            RuleFor(x => x).Custom((shipmentContext, context) =>
            {
                if (string.IsNullOrWhiteSpace(shipmentContext.Shipment.ShipmentMethodCode))
                {
                    context.AddFailure(new CartValidationError(shipmentContext.Shipment, "ShipmentMethodCode is Mandatory", "SHIPMENT_METHOD_CODE_IS_MANDATORY"));
                }
            });
        }
    }
  1. Register the new validator inside you module Module.cs
    public void PostInitialize(IApplicationBuilder appBuilder)
    {
        AbstractTypeFactory<CartShipmentValidator>.OverrideType<CartShipmentValidator, CartShipmentValidator2>();
    }

Upon calling AddOrUpdateCartShipment the validators will be executed in the following sequence: CartShipmentValidator -> CartShipmentValidator2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Comments