Skip to content

[Shipping labels] Improve address validation errors #14468

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

Merged
merged 19 commits into from
Aug 20, 2025

Conversation

irfano
Copy link
Contributor

@irfano irfano commented Aug 13, 2025

Closes WOOMOB-904

Description

This PR adds support for displaying address field errors on the Edit Address screen, using the validation errors returned from the endpoint. For example, when the house number is missing, the API response looks like this:

 "errors": {
      "address": "House number is missing",
      "general": "Address not found"
    },

Steps to reproduce

  1. Go to Orders.
  2. Select an order that is eligible for creating shipping labels.
  3. Tap the Create shipping label button.
  4. Tap the Shipment details button to open the bottom sheet.
  5. Tap the Edit button next to "Ship to".
  6. Modify the address and enter an invalid one (e.g., omit the house number).
  7. Tap "Validate & Save".

Testing information

Repeat the steps for the origin address as well, but ensure that unverified addresses can't be saved as origin addresses.

The tests that have been performed

Steps above

Images/gif

Before After
Screenshot_20250814_000502 Screenshot_20250813_235844
  • I have considered if this change warrants release notes and have added them to RELEASE-NOTES.txt if necessary. Use the "[Internal]" label for non-user-facing changes.

irfano added 11 commits August 13, 2025 22:32
This commit updates the `AddressNormalizationModel` to include a new `errors` field.
The `WooShippingNetworkingMapper` is also updated to map the errors from the DTO to the model.
The `NormalizeAddressException` class is updated to accept a map of errors instead of a single error string. This allows for more granular error reporting, specifically differentiating between general errors and address-specific errors.
The error handling in `NormalizeAddress.kt` is updated to provide more specific error messages.
- If the response model is null, an "Empty response" message is used.
- `NormalizeAddressException` is now used when there are specific address errors.
- A general `Exception` is used for other errors, incorporating the error message from the response if available.
The `AddressStatus` enum has been converted to a sealed class. This change allows for more detailed state representation, particularly for the `VerifyFailed` status, which can now optionally include a `NormalizeAddressException`.
@irfano irfano added this to the 23.1 milestone Aug 13, 2025
@irfano irfano added feature: shipping labels Related to creating, ordering, or printing shipping labels. Task (new) labels Aug 13, 2025
@dangermattic
Copy link
Collaborator

dangermattic commented Aug 13, 2025

2 Warnings
⚠️ This PR is larger than 300 lines of changes. Please consider splitting it into smaller PRs for easier and faster reviews.
⚠️ This PR is assigned to the milestone 23.1. This milestone is due in less than 2 days.
Please make sure to get it merged by then or assign it to a milestone with a later deadline.

Generated by 🚫 Danger

@irfano irfano force-pushed the issue/WOOMOB-904-improve-address-validation-errors branch 2 times, most recently from edd4641 to 70a6a58 Compare August 13, 2025 21:23
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created this class to address the LargeClass Detekt error in the WooShippingEditAddressViewModel class.

@@ -12,22 +12,30 @@ class NormalizeAddress @Inject constructor(
private val site: SelectedSite,
) {
suspend operator fun invoke(address: Address): Result<AddressNormalizationModel> {
return site.getOrNull()?.let {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed site.getOrNull() to site.get() to align with the approach we use for our domain classes in the shipping label screens.

@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Aug 13, 2025

📲 You can test the changes from this Pull Request in WooCommerce-Wear Android by scanning the QR code below to install the corresponding build.
App Name WooCommerce-Wear Android
Platform⌚️ Wear OS
FlavorJalapeno
Build TypeDebug
Commit105114e
Direct Downloadwoocommerce-wear-prototype-build-pr14468-105114e.apk

@irfano irfano force-pushed the issue/WOOMOB-904-improve-address-validation-errors branch from 70a6a58 to 4b1fc71 Compare August 13, 2025 21:35
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Aug 13, 2025

📲 You can test the changes from this Pull Request in WooCommerce Android by scanning the QR code below to install the corresponding build.

App Name WooCommerce Android
Platform📱 Mobile
FlavorJalapeno
Build TypeDebug
Commit105114e
Direct Downloadwoocommerce-prototype-build-pr14468-105114e.apk

@codecov-commenter
Copy link

codecov-commenter commented Aug 14, 2025

Codecov Report

❌ Patch coverage is 60.79295% with 89 lines in your changes missing coverage. Please review.
✅ Project coverage is 37.90%. Comparing base (204ac0a) to head (105114e).
⚠️ Report is 32 commits behind head on trunk.

Files with missing lines Patch % Lines
...glabels/address/WooShippingEditAddressViewModel.kt 48.38% 25 Missing and 23 partials ⚠️
...orders/wooshippinglabels/address/AddressSection.kt 0.00% 14 Missing ⚠️
...ders/wooshippinglabels/address/NormalizeAddress.kt 33.33% 9 Missing and 3 partials ⚠️
...nglabels/networking/WooShippingNetworkingMapper.kt 0.00% 5 Missing ⚠️
.../wooshippinglabels/address/EditAddressViewState.kt 94.36% 1 Missing and 3 partials ⚠️
...oid/ui/orders/wooshippinglabels/ShipmentDetails.kt 0.00% 2 Missing ⚠️
...oid/ui/orders/wooshippinglabels/networking/DTOs.kt 0.00% 2 Missing ⚠️
...inglabels/networking/WooShippingLabelRepository.kt 0.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##              trunk   #14468      +/-   ##
============================================
- Coverage     37.91%   37.90%   -0.02%     
- Complexity     9329     9333       +4     
============================================
  Files          2017     2018       +1     
  Lines        113250   113293      +43     
  Branches      14985    15005      +20     
============================================
+ Hits          42942    42944       +2     
- Misses        66392    66413      +21     
- Partials       3916     3936      +20     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hichamboushaba hichamboushaba self-assigned this Aug 15, 2025
Copy link
Member

@hichamboushaba hichamboushaba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @irfano, works well.
I just left a question, but I'm pre-approving.

Comment on lines 31 to 34
val generalError: String?
get() = errors[ERROR_GENERAL]
val addressError: String?
get() = errors[ERROR_ADDRESS]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a remark here, the iOS app exposes an error for the name field (here), and the plugin takes this a step further by accepting an error for all fields (Check lines 43 to 49 of the file address-step/fields.jsx).

I'm not sure how to trigger any of the other errors, so I'm not sure if we need to support this or not, WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't add errors for the other fields because I couldn't reproduce them. I reviewed address-step/fields.jsx but still couldn't determine the mapping. In this PR, I mapped address key to the "Address" field, but I couldn't find the keys for the other fields.
@itsmeichigo, since you used name for the "Name" field errors, have you found a way to reproduce the name error?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed address-step/fields.jsx but still couldn't determine the mapping

You can find all the keys by looking for getProps on the file: name, company, email, phone, country, address, city, state and postcode. The mapping with of the error with the key is done on line 43.

I didn't add errors for the other fields because I couldn't reproduce them

IMO we can add them without being able to reproduce them, in case the errors happen for some specific countries, but that's now a blocker for merging this PR.

Copy link
Contributor Author

@irfano irfano Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your input, @hichamboushaba! I’ve added errors for name, company, email, phone, city, and postcode. I think it’s worth testing again, could you test it once more?

Screenshot_20250819_202713

I couldn’t test the errors with API Faker, so I used the Network Inspector’s Rules tab with the following mocked response:

{
  "data": {
    "success": false,
    "errors": {
      "name": "Testing name error",
      "company": "Testing company error",
      "address": "House number is missing",
      "city": "Testing city error",
      "state": "Testing state error",
      "postcode": "Testing postcode error",
      "email": "Testing email error",
      "phone": "Testing phone error",
      "general": "Address not found"
    },
    "isTrivialNormalization": false,
    "address": {
      "address_1": "60 29TH STREET #343",
      "address_2": "",
      "city": "SAN FRANCISCO",
      "company": "",
      "country": "US",
      "default_address": false,
      "email": "[email protected]",
      "is_verified": false,
      "name": "IRFAN OMUR",
      "phone": "",
      "postcode": "94110-4929",
      "state": "CA"
    }
  }
}

I didn't add test for country and state. Country field is always dropdown, so it's not necessary to add error for it. state can be editable for some cases but I decided not to add error for it as well since it's rarely not a dropdown, effort to add error for state field was larger and I didn't want to increase the scope of this PR. Plus we aren't even sure server send errors for states. Let me know if you think we should add it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @irfano, I tested the changes using ApiFaker, and it works as expected.

I didn't add test for country and state. Country field is always dropdown, so it's not necessary to add error for it. state can be editable for some cases but I decided not to add error for it as well since it's rarely not a dropdown, effort to add error for state field was larger and I didn't want to increase the scope of this PR. Plus we aren't even sure server send errors for states. Let me know if you think we should add it.

Sounds good to me.

@irfano irfano enabled auto-merge August 20, 2025 10:00
@irfano irfano merged commit 278279d into trunk Aug 20, 2025
17 checks passed
@irfano irfano deleted the issue/WOOMOB-904-improve-address-validation-errors branch August 20, 2025 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature: shipping labels Related to creating, ordering, or printing shipping labels. Task (new)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants