Skip to content

Commit 22b57b6

Browse files
committed
Update phone validation for non-US countries
1 parent ad00424 commit 22b57b6

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/Create Shipping Label Form/Shipping Address Validation/ShippingLabelAddressFormViewModel.swift

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,23 +181,38 @@ extension ShippingLabelAddressFormViewModel {
181181
case invalidPhoneNumber
182182
}
183183

184-
/// Validates phone number for origin address.
184+
/// Validates phone number for the address.
185185
/// This take into account whether phone is not empty,
186186
/// has length 10 with additional "1" area code for US.
187187
///
188-
/// Note: This logic may need to be updated if there is a need for validating other cases.
189-
///
190188
private var isPhoneNumberValid: Bool {
191189
guard let phone = address?.phone, phone.isNotEmpty else {
192190
return false
193191
}
192+
guard address?.country == "US" else {
193+
return true
194+
}
194195
if phone.hasPrefix("1") {
195196
return phone.count == 11
196197
} else {
197198
return phone.count == 10
198199
}
199200
}
200201

202+
private var phoneValidationError: ValidationError? {
203+
guard let addressToBeValidated = address else {
204+
return nil
205+
}
206+
if phoneNumberRequired {
207+
if addressToBeValidated.phone.isEmpty {
208+
return .missingPhoneNumber
209+
} else if !isPhoneNumberValid {
210+
return .invalidPhoneNumber
211+
}
212+
}
213+
return nil
214+
}
215+
201216
private func validateAddressLocally() -> [ValidationError] {
202217
var errors: [ValidationError] = []
203218

@@ -220,12 +235,8 @@ extension ShippingLabelAddressFormViewModel {
220235
if addressToBeValidated.country.isEmpty {
221236
errors.append(.country)
222237
}
223-
if phoneNumberRequired {
224-
if addressToBeValidated.phone.isEmpty {
225-
errors.append(.missingPhoneNumber)
226-
} else if !isPhoneNumberValid {
227-
errors.append(.invalidPhoneNumber)
228-
}
238+
if let error = phoneValidationError {
239+
errors.append(error)
229240
}
230241
}
231242

0 commit comments

Comments
 (0)