From 2f5fe5fe0be230c4c54f82fe8bfd21edf790ae67 Mon Sep 17 00:00:00 2001 From: mnd Date: Mon, 15 Dec 2025 12:13:31 +0100 Subject: [PATCH 1/5] feat(easy-domain): add VAT with testing --- packages/easy-domain/src/values/VAT.ts | 17 +++++++ packages/easy-domain/test/values/VAT.test.ts | 52 ++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 packages/easy-domain/src/values/VAT.ts create mode 100644 packages/easy-domain/test/values/VAT.test.ts diff --git a/packages/easy-domain/src/values/VAT.ts b/packages/easy-domain/src/values/VAT.ts new file mode 100644 index 000000000..caccfd6bc --- /dev/null +++ b/packages/easy-domain/src/values/VAT.ts @@ -0,0 +1,17 @@ +import { ctx, isNotEmpty, text, Value, replaceAll } from '@thisisagile/easy'; +import { isVAT } from 'validator'; + +export class VAT extends Value { + constructor(vat?: unknown) { + const cleaned = replaceAll(text(vat).trim, '.', ''); + super(cleaned.toString()); + } + + get isValid(): boolean { + const shopCode = (ctx.request as any)?.shopCode; + if (!shopCode || !isNotEmpty(this.value)) return false; + return isVAT(this.value, shopCode); + } +} + +export const toVAT = (vat?: unknown): VAT => new VAT(vat); diff --git a/packages/easy-domain/test/values/VAT.test.ts b/packages/easy-domain/test/values/VAT.test.ts new file mode 100644 index 000000000..48e964f70 --- /dev/null +++ b/packages/easy-domain/test/values/VAT.test.ts @@ -0,0 +1,52 @@ +import { ctx } from '@thisisagile/easy'; +import { toVAT, VAT } from '../../src/values/VAT'; +import '@thisisagile/easy-test'; + +describe('VAT', () => { + beforeEach(() => { + (ctx as any).request = { shopCode: 'NL' }; + }); + + afterEach(() => { + (ctx as any).request = undefined; + }); + + test('creates a VAT value', () => { + const vat = toVAT('NL123456789B01'); + expect(vat).toBeInstanceOf(VAT); + }); + + test('normalizes dots and trims input', () => { + const vat = toVAT(' NL12.34.56.78.9B01 '); + expect(vat.value).toBe('NL123456789B01'); + }); + + test('is valid for a correct VAT and shopCode', () => { + const vat = toVAT('NL123456789B01'); + expect(vat.isValid).toBeTruthy(); + }); + + test('is invalid without shopCode in ctx', () => { + (ctx as any).request = {}; + const vat = toVAT('NL123456789B01'); + expect(vat.isValid).toBeFalsy(); + }); + + test('is invalid for an incorrect VAT', () => { + const vat = toVAT('NL123'); + expect(vat.isValid).toBeFalsy(); + }); + + test('is invalid for empty input', () => { + const vat = toVAT(''); + expect(vat.isValid).toBeFalsy(); + }); + + test('respects shopCode country', () => { + (ctx as any).request = { shopCode: 'BE' }; + const vatNL = toVAT('NL 8128.93.773.B.01'); + const vatBE = toVAT('BE 0895.244.474'); + expect(vatNL.isValid).toBeFalsy(); + expect(vatBE.isValid).toBeTruthy(); + }); +}); From 9ea2b764c4cfbf963d33be6fd94bf2e51cdc9774 Mon Sep 17 00:00:00 2001 From: mnd Date: Mon, 15 Dec 2025 12:16:21 +0100 Subject: [PATCH 2/5] feat(easy-domain): add VAT to index --- packages/easy-domain/src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/easy-domain/src/index.ts b/packages/easy-domain/src/index.ts index d77b4e934..f973f94e9 100644 --- a/packages/easy-domain/src/index.ts +++ b/packages/easy-domain/src/index.ts @@ -18,3 +18,4 @@ export * from './values/DunsNumber'; export * from './values/PostalCode'; export * from './values/Slug'; export * from './values/Url'; +export * from './values/VAT'; From e07df9c9500625b0f0ab82d34b8557e43dd38df6 Mon Sep 17 00:00:00 2001 From: mnd Date: Mon, 15 Dec 2025 12:25:54 +0100 Subject: [PATCH 3/5] feat(easy-domain): shortening import in VAT test --- packages/easy-domain/test/values/VAT.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/easy-domain/test/values/VAT.test.ts b/packages/easy-domain/test/values/VAT.test.ts index 48e964f70..939630302 100644 --- a/packages/easy-domain/test/values/VAT.test.ts +++ b/packages/easy-domain/test/values/VAT.test.ts @@ -1,5 +1,5 @@ import { ctx } from '@thisisagile/easy'; -import { toVAT, VAT } from '../../src/values/VAT'; +import { toVAT, VAT } from '../../src'; import '@thisisagile/easy-test'; describe('VAT', () => { From d8b8ca4f7d94d33db0d079f15291598bdbaea3d0 Mon Sep 17 00:00:00 2001 From: mnd Date: Mon, 15 Dec 2025 13:33:37 +0100 Subject: [PATCH 4/5] fix(easy-domain): refactor VAT class to accept country in constructor --- packages/easy-domain/src/values/VAT.ts | 14 +++--- packages/easy-domain/test/values/VAT.test.ts | 50 ++++++++------------ 2 files changed, 28 insertions(+), 36 deletions(-) diff --git a/packages/easy-domain/src/values/VAT.ts b/packages/easy-domain/src/values/VAT.ts index caccfd6bc..96c01621c 100644 --- a/packages/easy-domain/src/values/VAT.ts +++ b/packages/easy-domain/src/values/VAT.ts @@ -1,17 +1,19 @@ -import { ctx, isNotEmpty, text, Value, replaceAll } from '@thisisagile/easy'; +import { isNotEmpty, text, Value, replaceAll } from '@thisisagile/easy'; import { isVAT } from 'validator'; export class VAT extends Value { - constructor(vat?: unknown) { + private readonly country: string; + + constructor(vat?: unknown, country = 'NL') { const cleaned = replaceAll(text(vat).trim, '.', ''); super(cleaned.toString()); + this.country = country; } get isValid(): boolean { - const shopCode = (ctx.request as any)?.shopCode; - if (!shopCode || !isNotEmpty(this.value)) return false; - return isVAT(this.value, shopCode); + if (!isNotEmpty(this.value)) return false; + return isVAT(this.value, this.country); } } -export const toVAT = (vat?: unknown): VAT => new VAT(vat); +export const vat = (v?: unknown, country?: string): VAT => new VAT(v, country); diff --git a/packages/easy-domain/test/values/VAT.test.ts b/packages/easy-domain/test/values/VAT.test.ts index 939630302..4c0d9f1a3 100644 --- a/packages/easy-domain/test/values/VAT.test.ts +++ b/packages/easy-domain/test/values/VAT.test.ts @@ -1,52 +1,42 @@ -import { ctx } from '@thisisagile/easy'; -import { toVAT, VAT } from '../../src'; +import { vat, VAT } from '../../src'; import '@thisisagile/easy-test'; describe('VAT', () => { - beforeEach(() => { - (ctx as any).request = { shopCode: 'NL' }; - }); - - afterEach(() => { - (ctx as any).request = undefined; - }); test('creates a VAT value', () => { - const vat = toVAT('NL123456789B01'); - expect(vat).toBeInstanceOf(VAT); + const v = vat('NL 123456...789B01', 'NL'); + expect(v).toBeInstanceOf(VAT); }); test('normalizes dots and trims input', () => { - const vat = toVAT(' NL12.34.56.78.9B01 '); - expect(vat.value).toBe('NL123456789B01'); + const v = vat(' NL12.34.56.78.9B01 ', 'NL'); + expect(v.value).toBe('NL123456789B01'); }); - test('is valid for a correct VAT and shopCode', () => { - const vat = toVAT('NL123456789B01'); - expect(vat.isValid).toBeTruthy(); - }); - - test('is invalid without shopCode in ctx', () => { - (ctx as any).request = {}; - const vat = toVAT('NL123456789B01'); - expect(vat.isValid).toBeFalsy(); + 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 vat = toVAT('NL123'); - expect(vat.isValid).toBeFalsy(); + const v = vat('NL123', 'NL'); + expect(v.isValid).toBeFalsy(); }); test('is invalid for empty input', () => { - const vat = toVAT(''); - expect(vat.isValid).toBeFalsy(); + const v = vat('', 'NL'); + expect(v.isValid).toBeFalsy(); }); - test('respects shopCode country', () => { - (ctx as any).request = { shopCode: 'BE' }; - const vatNL = toVAT('NL 8128.93.773.B.01'); - const vatBE = toVAT('BE 0895.244.474'); + 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(); + }); }); From dc8997da4e7b0a4f68a2895fea9f40002b98ff74 Mon Sep 17 00:00:00 2001 From: mnd Date: Mon, 15 Dec 2025 16:06:39 +0100 Subject: [PATCH 5/5] fix(easy-domain): require VAT value and normalize country to uppercase while recleaning VAT input --- packages/easy-domain/src/values/VAT.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/easy-domain/src/values/VAT.ts b/packages/easy-domain/src/values/VAT.ts index 96c01621c..cbea8ff73 100644 --- a/packages/easy-domain/src/values/VAT.ts +++ b/packages/easy-domain/src/values/VAT.ts @@ -1,13 +1,14 @@ -import { isNotEmpty, text, Value, replaceAll } from '@thisisagile/easy'; +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 = replaceAll(text(vat).trim, '.', ''); - super(cleaned.toString()); - this.country = country; + constructor(vat: unknown, country = 'NL') { + const cleaned = text(vat).trim.replace('.', '').toString(); + super(cleaned); + + this.country = text(country).trim.upper.toString(); } get isValid(): boolean { @@ -16,4 +17,4 @@ export class VAT extends Value { } } -export const vat = (v?: unknown, country?: string): VAT => new VAT(v, country); +export const vat = (v: unknown, country?: string): VAT => new VAT(v, country);