Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/easy-domain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './values/DunsNumber';
export * from './values/PostalCode';
export * from './values/Slug';
export * from './values/Url';
export * from './values/VAT';
20 changes: 20 additions & 0 deletions packages/easy-domain/src/values/VAT.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { isNotEmpty, text, Value } from '@thisisagile/easy';
import { isVAT } from 'validator';

export class VAT extends Value {
private readonly country: string;

constructor(vat: unknown, country = 'NL') {
const cleaned = text(vat).trim.replace('.', '').toString();
super(cleaned);

this.country = text(country).trim.upper.toString();
}

get isValid(): boolean {
if (!isNotEmpty(this.value)) return false;
return isVAT(this.value, this.country);
}
}

export const vat = (v: unknown, country?: string): VAT => new VAT(v, country);
42 changes: 42 additions & 0 deletions packages/easy-domain/test/values/VAT.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { vat, VAT } from '../../src';
import '@thisisagile/easy-test';

describe('VAT', () => {

test('creates a VAT value', () => {
const v = vat('NL 123456...789B01', 'NL');
expect(v).toBeInstanceOf(VAT);
});

test('normalizes dots and trims input', () => {
const v = vat(' NL12.34.56.78.9B01 ', 'NL');
expect(v.value).toBe('NL123456789B01');
});

test('is valid for a correct VAT and country', () => {
const v = vat('NL123456789B01', 'NL');
expect(v.isValid).toBeTruthy();
});

test('is invalid for an incorrect VAT', () => {
const v = vat('NL123', 'NL');
expect(v.isValid).toBeFalsy();
});

test('is invalid for empty input', () => {
const v = vat('', 'NL');
expect(v.isValid).toBeFalsy();
});

test('respects country code', () => {
const vatNL = vat('NL 8128.93.773.B.01', 'BE');
const vatBE = vat('BE 0895.244.474', 'BE');
expect(vatNL.isValid).toBeFalsy();
expect(vatBE.isValid).toBeTruthy();
});

test('defaults to NL if country not provided', () => {
const v = vat('NL123456789B01');
expect(v.isValid).toBeTruthy();
});
});
Loading