se-panfilov / jsvat

Check the validity of the format of an EU VAT number

Home Page:http://se-panfilov.github.io/jsvat

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Check VAT number using a specified country code.

RudeySH opened this issue · comments

How do I check if a specific VAT number is valid on the country "NL"?

I should clarify that I understand that you can specify English country names, but there is no way to specify country code. That really seems like an oversight, any non-English application will have to resort to using a switch that converts country codes to english country names just to work with this library.

@rlenders Sorry, I'm not sure if I catch the problem.
Can you please provide an example?

I mean it would be better if you'll provide it like "What we have now: ..., What I want to see: ..."

Usually if I have a NL's VAT, like NL010000446B01, an output will be like {"value":"NL010000446B01","isValid":true,"country":"netherlands"}.

E.g. If I want to allow only Netherland's VAT I can check result's field "country" to be equal to netherlands.

Basically

function checkVAT(vat){
var result = jsvat.checkVAT(vat)
if (result.isValid && result.country === 'netherlands') return true
return false
}

checkVAT('NL010000446B01')

If your VATs have no country code prefixes... well I'm not sure if it will be possible to recognise for which country this VAT is belongs.

Perhaps you can try just to provide country prefix:

const countries = {
  netherlands: 'NL',
  russia: 'RU'
  //...
}

function checkVAT(vat, prefix){
var result = jsvat.checkVAT(prefix + vat)
if (result.isValid && result.country === 'netherlands') return true
return false
}

checkVAT('010000446B01',  countries.netherlands)

As an example, say my application requires a user to select their country from a localized dropdown, and enter their VAT number, which will be validated if the country is supported by your library. It is not allowed to use a VAT number from a different country than the one selected.

To achieve this using this library, I will have to provide a mapping between my dropdown's ISO-standard country codes and this library's non-standard country names. I can't simply .toLowerCase() my country names because netherlands could be written as "The Netherlands", "Holland", "Nederland" or any other localized version. So I need to cook up my own mapping like you suggested (const countries).

I feel like this should not be necessary. The list of supported countries is not too long, but if each developer has to supply their own mapping, mistakes will be made. Also, if this library is updated tomorrow and a new country is added, the mapping I wrote will be incomplete. If maintainers update my application to use the latest version of this library, there's a possibility they don't realize some custom mapping requires to be updated as well.

This hurdle can easily be eliminated by supporting or even using country codes internally, like so:
jsvat.countryCodes = ['BE', 'DE', 'LU', 'NL']

Furthermore, this would be nice to have too: if (jsvat.isCountryCodeSupported(cc)) { ... }

@rlenders Got it. Work in progress

@rlenders You can use iso codes for comparison right now.

So first take a look at updated readme.

You can block several countries

jsvat.blocked = ['BE', 'Russia', 'GRC', '203'] //You can provide country's name, or one of iso codes

Also you can block ALL countries except listed:
You can block several countries

jsvat.allowed = ['BE', 'Russia', 'GRC', '203']

End I've changed signature of checkVAT()'s result:

{
  value: 'BG131134023', // VAT without extra characters (like '-' and spaces)
  isValid: true, 
  country: { // VAT's couuntry (null if not found)
      name: country.name, //Name of the country
      isoCode: { //Country ISO codes
        short: 'BE', 
        long: 'BEL',
        numeric: '056' //String, because of forwarding zero
      }
    }
  }

@rlenders is it okay for you?

Seems great so far! Good call on supporting multiple ISO standards. Thanks a bunch.

@rlenders Cool than, feel free to ping me in case of anything

sorry to reopen this, but I'm not quit sure if I understand this correctly:

If I have a user-entered VAT and also a preselected country (with ISO code),
I need to set the jsvat.allowed = ['myIsoCode']; prior of calling jsvat.checkVat("userEnteredVat"); ?

Wouldn't it be an easy thing to simply provide an overload for checkVat which accepts an iso code / iso code array as 2nd parameter?

Some pseudocode:

function checkVat(vat: string, isoCode: string) {
    const country = GetCountryByIsoCode(isoCode);
    return this.checkVat(vat, [country]);
}