11import React , { useCallback , useEffect , useRef } from 'react' ;
22import { View } from 'react-native' ;
33import FormProvider from '@components/Form/FormProvider' ;
4- import type { FormInputErrors , FormOnyxKeys , FormOnyxValues , FormRef , FormValue } from '@components/Form/types' ;
4+ import type { FormInputErrors , FormOnyxKeys , FormOnyxValues , FormRef } from '@components/Form/types' ;
55import PatriotActLink from '@components/PatriotActLink' ;
66import Text from '@components/Text' ;
77import useLocalize from '@hooks/useLocalize' ;
88import type { SubStepProps } from '@hooks/useSubStep/types' ;
99import useThemeStyles from '@hooks/useThemeStyles' ;
1010import type { ForwardedFSClassProps } from '@libs/Fullstory/types' ;
11- import { getFieldRequiredErrors , getInvalidAddressErrorTranslationPath , isValidZipCode , isValidZipCodeInternational } from '@libs/ValidationUtils' ;
11+ import { getCountryZipRegexDetails , getFieldRequiredErrors , getInvalidAddressErrorTranslationPath , isValidZipCode , isValidZipCodeForCountry } from '@libs/ValidationUtils' ;
1212import AddressFormFields from '@pages/ReimbursementAccount/AddressFormFields' ;
1313import HelpLinks from '@pages/ReimbursementAccount/USD/Requestor/PersonalInfo/HelpLinks' ;
1414import { setDraftValues } from '@userActions/FormActions' ;
@@ -32,6 +32,15 @@ type AddressInputIDs = {
3232 country ?: string ;
3333} ;
3434
35+ function getStringFormValue < TFormID extends keyof OnyxFormValuesMapping > ( values : FormOnyxValues < TFormID > , fieldID ?: string ) : string {
36+ if ( ! fieldID ) {
37+ return '' ;
38+ }
39+
40+ const value = values [ fieldID as keyof FormOnyxValues < TFormID > ] ;
41+ return typeof value === 'string' ? value : '' ;
42+ }
43+
3544type AddressStepProps < TFormID extends keyof OnyxFormValuesMapping > = SubStepProps &
3645 ForwardedFSClassProps & {
3746 /** The ID of the form */
@@ -138,23 +147,29 @@ function AddressStep<TFormID extends keyof OnyxFormValuesMapping>({
138147 ( values : FormOnyxValues < TFormID > ) : FormInputErrors < TFormID > => {
139148 const errors = getFieldRequiredErrors ( values , stepFields , translate ) ;
140149
141- const street = values [ inputFieldsIDs . street as keyof typeof values ] ;
142- const streetValue = street as FormValue ;
143- const streetError = getInvalidAddressErrorTranslationPath ( streetValue ) ;
150+ const street = getStringFormValue ( values , inputFieldsIDs . street ) ;
151+ const streetError = getInvalidAddressErrorTranslationPath ( street ) ;
144152 if ( street && streetError ) {
145153 // @ts -expect-error type mismatch to be fixed
146154 errors [ inputFieldsIDs . street ] = translate ( streetError ) ;
147155 }
148156
149- const zipCode = values [ inputFieldsIDs . zipCode as keyof typeof values ] ;
150- if ( shouldValidateZipCodeFormat && zipCode && ( shouldDisplayCountrySelector ? ! isValidZipCodeInternational ( zipCode as string ) : ! isValidZipCode ( zipCode as string ) ) ) {
157+ const zipCode = getStringFormValue ( values , inputFieldsIDs . zipCode ) ;
158+ const selectedCountry = ( inputFieldsIDs . country ? getStringFormValue ( values , inputFieldsIDs . country ) : defaultValues . country ) as Country | '' ;
159+ const shouldValidateSelectedCountryZip = shouldDisplayCountrySelector && ! ! inputFieldsIDs . country ;
160+
161+ if ( zipCode && shouldValidateSelectedCountryZip && ! isValidZipCodeForCountry ( zipCode , selectedCountry ) ) {
162+ const zipCodeSamples = getCountryZipRegexDetails ( selectedCountry ) ?. samples ;
163+ // @ts -expect-error type mismatch to be fixed
164+ errors [ inputFieldsIDs . zipCode ] = translate ( 'privatePersonalDetails.error.incorrectZipFormat' , zipCodeSamples ) ;
165+ } else if ( zipCode && shouldValidateZipCodeFormat && ! isValidZipCode ( zipCode ) ) {
151166 // @ts -expect-error type mismatch to be fixed
152167 errors [ inputFieldsIDs . zipCode ] = translate ( 'bankAccount.error.zipCode' ) ;
153168 }
154169
155170 return errors ;
156171 } ,
157- [ inputFieldsIDs . street , inputFieldsIDs . zipCode , shouldDisplayCountrySelector , shouldValidateZipCodeFormat , stepFields , translate ] ,
172+ [ defaultValues . country , inputFieldsIDs . country , inputFieldsIDs . street , inputFieldsIDs . zipCode , shouldDisplayCountrySelector , shouldValidateZipCodeFormat , stepFields , translate ] ,
158173 ) ;
159174
160175 return (
@@ -182,7 +197,6 @@ function AddressStep<TFormID extends keyof OnyxFormValuesMapping>({
182197 stateSelectorSearchInputTitle = { stateSelectorSearchInputTitle }
183198 onCountryChange = { onCountryChange }
184199 shouldAllowCountryChange = { shouldAllowCountryChange }
185- shouldValidateZipCodeFormat = { shouldValidateZipCodeFormat }
186200 forwardedFSClass = { forwardedFSClass }
187201 />
188202 { ! ! shouldShowHelpLinks && (
0 commit comments