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.76k stars 218 forks source link

parsePhoneNumberWithError() return isValid() === 'false' but not throw an error #433

Closed ballonura closed 1 year ago

ballonura commented 1 year ago

Code:

import { parsePhoneNumberWithError } from "libphonenumber-js";

const run = () => {
  try {
    const phoneNumber = parsePhoneNumberWithError("12", "US");

    console.log(phoneNumber.number);
    console.log(phoneNumber.isValid()); // false
  } catch (error: any) {
    console.log(error.message);
  }
};

run();

How can i get the error/reason for the number to be invalid? Thx. ;)

catamphetamine commented 1 year ago

How can i get the error/reason for the number to be invalid?

The error contains the error, doesn't it

blaadje commented 1 year ago

+1, it does not throw error for TOO_SHORT case for example meanwhile isValid returns false (as expected)

catamphetamine commented 1 year ago

"Not valid" is not equal to "an error". "An error" would be "can't possibly be a phone number" rather than "looks like a phone number but is not a valid one".