Skip to content

Commit

Permalink
feat: add @IsISO4217CurrencyCode decorator (typestack#1824)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoNameProvided authored Dec 3, 2022
1 parent b564f8d commit 7fe37ed
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ isBoolean(value);
| `@IsByteLength(min: number, max?: number)` | Checks if the string's length (in bytes) falls in a range. |
| `@IsCreditCard()` | Checks if the string is a credit card. |
| `@IsCurrency(options?: IsCurrencyOptions)` | Checks if the string is a valid currency amount. |
| `@IsISO4217CurrencyCode()` | Checks if the string is an ISO 4217 currency code. |
| `@IsEthereumAddress()` | Checks if the string is an Ethereum address using basic regex. Does not validate address checksums. |
| `@IsBtcAddress()` | Checks if the string is a valid BTC address. |
| `@IsDataURI()` | Checks if the string is a data uri format. |
Expand Down
1 change: 1 addition & 0 deletions src/decorator/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export * from './string/IsStrongPassword';
export * from './string/IsTimeZone';
export * from './string/IsBase58';
export * from './string/is-tax-id';
export * from './string/is-iso4217-currency-code';

// -------------------------------------------------------------------------
// Type checkers
Expand Down
31 changes: 31 additions & 0 deletions src/decorator/string/is-iso4217-currency-code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ValidationOptions } from '../ValidationOptions';
import { buildMessage, ValidateBy } from '../common/ValidateBy';
import isISO4217Validator from 'validator/lib/isISO4217';

export const IS_ISO4217_CURRENCY_CODE = 'isISO4217CurrencyCode';

/**
* Check if the string is a valid [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) officially assigned currency code.
*/
export function isISO4217CurrencyCode(value: unknown): boolean {
return typeof value === 'string' && isISO4217Validator(value);
}

/**
* Check if the string is a valid [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) officially assigned currency code.
*/
export function IsISO4217CurrencyCode(validationOptions?: ValidationOptions): PropertyDecorator {
return ValidateBy(
{
name: IS_ISO4217_CURRENCY_CODE,
validator: {
validate: (value, args): boolean => isISO4217CurrencyCode(value),
defaultMessage: buildMessage(
eachPrefix => eachPrefix + '$property must be a valid ISO4217 currency code',
validationOptions
),
},
},
validationOptions
);
}
18 changes: 18 additions & 0 deletions test/functional/validation-functions-and-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ import {
isBase58,
isTaxId,
IsTaxId,
IsISO4217CurrencyCode,
} from '../../src/decorator/decorators';
import { Validator } from '../../src/validation/Validator';
import { ValidatorOptions } from '../../src/validation/ValidatorOptions';
Expand Down Expand Up @@ -4730,3 +4731,20 @@ describe('IsBase58', () => {
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
});
});

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

it('should not fail for a valid ISO4217 code', () => {
const validValues = ['EUR', 'USD', 'BDT', 'LRD'];
return checkValidValues(new MyClass(), validValues);
});

it('should fail for invalid values', () => {
const invalidValues = [undefined, null, '', 'USS'];
return checkInvalidValues(new MyClass(), invalidValues);
});
});

0 comments on commit 7fe37ed

Please sign in to comment.