Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No validateVat method #50

Open
dreinon opened this issue May 2, 2023 · 10 comments
Open

No validateVat method #50

dreinon opened this issue May 2, 2023 · 10 comments
Labels
enhancement New feature or request

Comments

@dreinon
Copy link

dreinon commented May 2, 2023

Hi!
Congrats for the implementation of this python-original module in TS! Really helpful!

I was wondering why there is a validatePerson and validateEntity method, but no validateVat method?
Thanks!

@koblas
Copy link
Owner

koblas commented May 2, 2023

Thanks!

I had given the validateVat method some thought. My original thinking was that it should validate it and return a list of countries that it was valid for. e.g.

function validateVat(value: string): string[];

Or something similar. I can see this being useful in the context of you having a VAT that you believe to be a EU VAT but you're uncertain which country or maybe you're attempting to validate that it matches a given country (e.g. company claims to be French but they give a German VAT).

What's the use case that you're thinking about?

@dreinon
Copy link
Author

dreinon commented May 3, 2023

In my case, we do have the country of the company, so we would just need to pass the country and a value to the function and get if it is valid or not. In case it is, get the formatted value.

@koblas
Copy link
Owner

koblas commented May 8, 2023

Thinking about this a bit more I wonder if this would be a solid solution. Which basically would restrict the "business" validators to things that are VAT codes. It would then be easy for you to write a specific function for your needs.

const vatValidators: Record<string, Validator> {
  // ...
  BE: BE.vat,
  BG: BG.vat,
  // ...
}

@dreinon
Copy link
Author

dreinon commented May 9, 2023

Hi @koblas, didn't really understand your proposal, sorry. Could you explain a bit more, please?

@koblas
Copy link
Owner

koblas commented May 10, 2023

Based on the current patterns in the code, it would make sense for the the code to just have a table for VAT validation for the given country. As you mentioned you already know the country so this would allow you to just have an implementation something like.

function vatFormat(country: string, vat: string): string | null {
     const validator = vatValidator[country];
     // VAT validation for country not found
     if (!validator) {
         return null;
     }
     // 
     const { isValid} = validator.validate(vat);
     if (!isValid) {
         return null;
     }
     return validator.format(vat);
}

@dreinon
Copy link
Author

dreinon commented May 11, 2023

Right, that would be a valid implementation! Don't we have euVat object already?

@koblas
Copy link
Owner

koblas commented May 11, 2023

Not presently, but that would be a simple add.

@dreinon
Copy link
Author

dreinon commented May 11, 2023

Isn't it this line?

@koblas
Copy link
Owner

koblas commented May 11, 2023

Good grief! Yes, it is. Though at least while I was writing this, I was thinking it was singular for VAT not an array.

@dreinon
Copy link
Author

dreinon commented May 12, 2023

Good grief! Yes, it is. Though at least while I was writing this, I was thinking it was singular for VAT not an array.

Right! Though the arrays in the values of the VAT object are 1-lenght arrays, therefore we could just add the following function, taking the example you posted:

function vatFormat(country: string, vat: string): string | undefined {
     const [validator] = vatValidator[country];

     // VAT validation for country not found
     if (!validator) return;

     const { isValid} = validator.validate(vat);
     if (!isValid) return;

     return validator.format(vat);
}

@koblas koblas added the enhancement New feature or request label Jun 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants