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.79k stars 216 forks source link

Use custom data in react native, but give the error `Metadata is required` #205

Closed annewanghy closed 6 years ago

annewanghy commented 6 years ago

Hi, really appreciate your library. I have a little problem in react native.

When I use the custom metadata in my project.

Since I use typescript, so I need to write a phonenumber.d.ts file to declare the function(And I have found there is no custom folder in node_modules/libphonenumber-js)

declare module "libphonenumber-js/custom" {
  export function isValidNumber(
    val: string,
    country?: string,
    metadata?: object
  ): boolean;
}

And then I use it as frontend validation

import { isValidNumber } from "libphonenumber-js/custom";
import metadata from "../metadata.min.json";

if (
      rules.isValidPhoneNumber &&
      !isValidNumber(valStr, "JP", metadata)
    ) {
      return "phone_number_invalid";
}

the code works well but the test cases give me the error

Error for phone: Error: Metadata is required

Each function related to phone validation will be broken due to this error, but I don't know how to fixed it. Can you help me with that? thanks in advance : )

catamphetamine commented 6 years ago

Error: Metadata is required means that either metadata argument was not passed or it was not a valid metadata.

catamphetamine commented 6 years ago

I guess I should change the error message to be more descriptive.

annewanghy commented 6 years ago

Debugging, thank you for the quick reply!

catamphetamine commented 6 years ago

Version 1.1.11 has been published with more descriptive error message in case of metadata argument being of incorrect type.

annewanghy commented 6 years ago

Had solved this problem by using const metadata = require("../metadata.min.json"); instead of import metadata from "../metadata.min.json"; since test environment is node, so it doesn't support es6.

catamphetamine commented 6 years ago

So, you changed your code and it worked? Can this issue be closed then?

annewanghy commented 6 years ago

Yes.