You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using Yup-phone & Yup on a react project to validate data of formik forms, but my phone field is not mandatory and could have a value undefined.
Using yup phone, when my phone field is undefined, Yup always considers it as an error but it is wrong. Reading the code, we should consider that value could be undefined, so the test does not fail.
That is an example of phone implement you could consider :
Yup.addMethod(Yup.string,'phone',function(countryCode?: string,strict=false,errorMessage=''){consterrMsg=typeoferrorMessage==='string'&&errorMessage
? errorMessage
: isValidCountryCode(countryCode)
? `\${path} must be a valid phone number for region ${countryCode}`
: '${path} must be a valid phone number.'returnthis.test('phone',errMsg,(value?: string)=>{let_countryCode=countryCodelet_strict=strictif(!isValidCountryCode(countryCode)){// if not valid countryCode, then set default country to India (IN)
_countryCode ='IN'_strict=false}try{if(value){constphoneNumber=phoneUtil.parseAndKeepRawInput(value,countryCode)if(!phoneUtil.isPossibleNumber(phoneNumber)){returnfalse}constregionCodeFromPhoneNumber=phoneUtil.getRegionCodeForNumber(phoneNumber)/* check if the countryCode provided should be used as default country code or strictly followed */return_strict ? phoneUtil.isValidNumberForRegion(phoneNumber,_countryCode) : phoneUtil.isValidNumberForRegion(phoneNumber,regionCodeFromPhoneNumber)}returntrue}catch{returnfalse}})})
I am using Yup-phone & Yup on a react project to validate data of formik forms, but my phone field is not mandatory and could have a value undefined.
Using yup phone, when my phone field is undefined, Yup always considers it as an error but it is wrong. Reading the code, we should consider that value could be undefined, so the test does not fail.
That is an example of phone implement you could consider :
Here is an example of my usage:
Thanks to my previous proposal, I have no error when phone is not mandatory (ie undefined)
The text was updated successfully, but these errors were encountered: