catamphetamine / libphonenumber-js

A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript
https://catamphetamine.gitlab.io/libphonenumber-js/
MIT License
2.77k stars 217 forks source link

parsePhoneNumberWithError `metadata` argument was passed but it's not a valid metadata #468

Open m2xilon opened 1 week ago

m2xilon commented 1 week ago

Getting an error when calling parsePhoneNumberWithError(mobileNumber, countryCode) in google cloud functions pubsub (using Cloud Functions v1) libphonenumber-js versions affected: 1.11.3, 1.11.7

When: Encountered it only yesterday but unsure when exactly it was introduced, though must have been recent.

What: This is the error we have been getting: [libphonenumber-js]metadataargument was passed but it's not a valid metadata. Must be an object having.countrieschild object property. Got an object of shape: { default }. at validateMetadata (/workspace/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/source/metadata.js:463:9) at Metadata (/workspace/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/source/metadata.js:23:3) at parse (/workspace/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/source/parse.js:72:13) at parsePhoneNumberWithError (/workspace/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/source/parsePhoneNumberWithError_.js:4:9) at parsePhoneNumberWithError (/workspace/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/source/parsePhoneNumberWithError.js:6:9) at call (/workspace/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/min/index.cjs.js:14:14) at parsePhoneNumberWithError (/workspace/node_modules/.pnpm/libphonenumber-js@1.11.7/node_modules/libphonenumber-js/min/index.cjs.js:31:9)

Code:

import { parsePhoneNumberWithError } from 'libphonenumber-js'
export const validatedMobileNumber = (input: string, countryCode: CountryCode) => {
try {
    const phoneNumber = parsePhoneNumberWithError(input, countryCode) // error occurs here
  } catch (error) {
    logger?.info(`Error when parsing ${input} with parsePhoneNumberWithError`, error.message)
    throw error
  }
  // code never reaches here
  return phoneNumber.format('E.164')
}

This same code when running from a js script with node works as expected. But when deployed throws the error.

A workaround for a related package has been found by @nikita.rybak here (see comment from 23rd of August): https://gitlab.com/catamphetamine/react-phone-number-input/-/issues/141

Using this

import { parsePhoneNumberWithError } from 'libphonenumber-js/core'
import phoneNumberMetadata from 'libphonenumber-js/mobile/metadata'
...
phoneNumber = parsePhoneNumberWithError(input, countryCode, phoneNumberMetadata)
...

Presently an issue for some using as per the readme.

catamphetamine commented 1 week ago

Hi. I see. Thx for the report. I dunno. Wontfix.

On Wed, 4 Sep 2024 at 05:53, m2xilon @.***> wrote:

Getting an error when calling parsePhoneNumberWithError(mobileNumber, countryCode) in google cloud functions pubsub libphonenumber-js versions affected: 1.11.3, 1.11.7

When: Encountered it only yesterday but unsure when exactly it was introduced, though must have been recent.

What: This is the error we have been getting: [libphonenumber-js] metadataargument was passed but it's not a valid metadata. Must be an object having.countries child object property. Got an object of shape: { default }. at validateMetadata @./node_modules/libphonenumber-js/source/metadata.js:463:9) at Metadata @./node_modules/libphonenumber-js/source/metadata.js:23:3) at parse @./node_modules/libphonenumber-js/source/parse.js:72:13) at parsePhoneNumberWithError @./nodemodules/libphonenumber-js/source/parsePhoneNumberWithError.js:4:9) at parsePhoneNumberWithError @./node_modules/libphonenumber-js/source/parsePhoneNumberWithError.js:6:9) at call @./node_modules/libphonenumber-js/min/index.cjs.js:14:14) at parsePhoneNumberWithError @.*** /node_modules/libphonenumber-js/min/index.cjs.js:31:9)

Code:

import { parsePhoneNumberWithError } from 'libphonenumber-js' export const validatedMobileNumber = (input: string, countryCode: CountryCode) => { try { const phoneNumber = parsePhoneNumberWithError(input, countryCode) // error occurs here } catch (error) { logger?.info(Error when parsing ${input} with parsePhoneNumberWithError, error.message) throw error } // code never reaches here return phoneNumber.format('E.164') }

This same code when running from a js script with node works as expected. But when deployed throws the error.

A workaround for a related package has been found by @nikita.rybak https://gitlab.com/nikita.rybak here (see comment from 23rd of August): https://gitlab.com/catamphetamine/react-phone-number-input/-/issues/141

Using this

import { parsePhoneNumberWithError } from 'libphonenumber-js/core' import phoneNumberMetadata from 'libphonenumber-js/mobile/metadata' ... phoneNumber = parsePhoneNumberWithError(input, countryCode, phoneNumberMetadata) ...

Presently an issue for some using as per the readme.

— Reply to this email directly, view it on GitHub https://github.com/catamphetamine/libphonenumber-js/issues/468, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADUP3ZQWC6YFUK2TW5TSLDZUZY2VAVCNFSM6AAAAABNTLDS5CVHI2DSMVQWIX3LMV43ASLTON2WKOZSGUYDIMJYGI4TSOI . You are receiving this because you are subscribed to this thread.Message ID: @.***>