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

Feature/add is vat id decorator #2497

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ isBoolean(value);
| `@IsSurrogatePair()` | Checks if the string contains any surrogate pairs chars. |
| `@IsTaxId()` | Checks if the string is a valid tax ID. Default locale is `en-US`. |
| `@IsUrl(options?: IsURLOptions)` | Checks if the string is a URL. |
| `@IsVatId(countryCode: string)` | Checks if the string is a [valid VAT identification number for the country](https://taxation-customs.ec.europa.eu/vat-identification-numbers_en). |
| `@IsMagnetURI()` | Checks if the string is a [magnet uri format](https://en.wikipedia.org/wiki/Magnet_URI_scheme). |
| `@IsUUID(version?: UUIDVersion)` | Checks if the string is a UUID (version 3, 4, 5 or all ). |
| `@IsFirebasePushId()` | Checks if the string is a [Firebase Push ID](https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html) |
Expand Down
1 change: 1 addition & 0 deletions src/decorator/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export * from './string/IsTimeZone';
export * from './string/IsBase58';
export * from './string/is-tax-id';
export * from './string/is-iso4217-currency-code';
export * from './string/IsVatId';

// -------------------------------------------------------------------------
// Type checkers
Expand Down
36 changes: 36 additions & 0 deletions src/decorator/string/IsVatId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ValidationOptions } from '../ValidationOptions';
import { buildMessage, ValidateBy } from '../common/ValidateBy';
import isVatIdValidator from 'validator/lib/isVat';

export const IS_VAT_ID = 'isVat';

/**
* Checks if the string is a VAT (value-added tax) identification number.
* If given value is not a string, then it returns false.
*/
export function isVatId(value: unknown, countryCode: string): boolean {
try {
const isVatId = typeof value === 'string' && isVatIdValidator(value, countryCode);
return isVatId;
} catch (error) {
return false;
}
}

/**
* Checks if the string is a VAT (value-added tax) identification number.
* If given value is not a string, then it returns false.
*/
export function IsVatId(countryCode?: string, validationOptions?: ValidationOptions): PropertyDecorator {
return ValidateBy(
{
name: IS_VAT_ID,
constraints: [countryCode],
validator: {
validate: (value, args): boolean => isVatId(value, args?.constraints[0]),
defaultMessage: buildMessage(eachPrefix => eachPrefix + '$property must be a VAT ID', validationOptions),
},
},
validationOptions
);
}
32 changes: 32 additions & 0 deletions test/functional/validation-functions-and-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ import {
isTaxId,
IsTaxId,
IsISO4217CurrencyCode,
IsVatId,
} from '../../src/decorator/decorators';
import { Validator } from '../../src/validation/Validator';
import { ValidatorOptions } from '../../src/validation/ValidatorOptions';
Expand Down Expand Up @@ -4779,3 +4780,34 @@ describe('IsISO4217', () => {
return checkInvalidValues(new MyClass(), invalidValues);
});
});

describe('IsVatId', () => {
class MyClass {
@IsVatId('AT')
someProperty: string;
}

class MyInvalidClass {
@IsVatId('ZZ')
someProperty: string;
}

const validAtVatIds = ['U12345678', 'U01234567'];
const invalidAtVatIds = ['12345678', '000', 'abcdefgh'];

it('should not fail for a valid VAT id for valid country code', () => {
return checkValidValues(new MyClass(), validAtVatIds);
});

it('should fail for invalid country code', () => {
return checkInvalidValues(new MyInvalidClass(), validAtVatIds);
});

it('should fail for invalid VAT id for valid country code', () => {
return checkInvalidValues(new MyClass(), invalidAtVatIds);
});

it('should fail for invalid VAT id for invalid country code', () => {
return checkInvalidValues(new MyInvalidClass(), invalidAtVatIds);
});
});