Skip to content

Commit 7fe37ed

Browse files
feat: add @IsISO4217CurrencyCode decorator (#1824)
1 parent b564f8d commit 7fe37ed

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@ isBoolean(value);
843843
| `@IsByteLength(min: number, max?: number)` | Checks if the string's length (in bytes) falls in a range. |
844844
| `@IsCreditCard()` | Checks if the string is a credit card. |
845845
| `@IsCurrency(options?: IsCurrencyOptions)` | Checks if the string is a valid currency amount. |
846+
| `@IsISO4217CurrencyCode()` | Checks if the string is an ISO 4217 currency code. |
846847
| `@IsEthereumAddress()` | Checks if the string is an Ethereum address using basic regex. Does not validate address checksums. |
847848
| `@IsBtcAddress()` | Checks if the string is a valid BTC address. |
848849
| `@IsDataURI()` | Checks if the string is a data uri format. |

src/decorator/decorators.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export * from './string/IsStrongPassword';
115115
export * from './string/IsTimeZone';
116116
export * from './string/IsBase58';
117117
export * from './string/is-tax-id';
118+
export * from './string/is-iso4217-currency-code';
118119

119120
// -------------------------------------------------------------------------
120121
// Type checkers
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { ValidationOptions } from '../ValidationOptions';
2+
import { buildMessage, ValidateBy } from '../common/ValidateBy';
3+
import isISO4217Validator from 'validator/lib/isISO4217';
4+
5+
export const IS_ISO4217_CURRENCY_CODE = 'isISO4217CurrencyCode';
6+
7+
/**
8+
* Check if the string is a valid [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) officially assigned currency code.
9+
*/
10+
export function isISO4217CurrencyCode(value: unknown): boolean {
11+
return typeof value === 'string' && isISO4217Validator(value);
12+
}
13+
14+
/**
15+
* Check if the string is a valid [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) officially assigned currency code.
16+
*/
17+
export function IsISO4217CurrencyCode(validationOptions?: ValidationOptions): PropertyDecorator {
18+
return ValidateBy(
19+
{
20+
name: IS_ISO4217_CURRENCY_CODE,
21+
validator: {
22+
validate: (value, args): boolean => isISO4217CurrencyCode(value),
23+
defaultMessage: buildMessage(
24+
eachPrefix => eachPrefix + '$property must be a valid ISO4217 currency code',
25+
validationOptions
26+
),
27+
},
28+
},
29+
validationOptions
30+
);
31+
}

test/functional/validation-functions-and-decorators.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ import {
192192
isBase58,
193193
isTaxId,
194194
IsTaxId,
195+
IsISO4217CurrencyCode,
195196
} from '../../src/decorator/decorators';
196197
import { Validator } from '../../src/validation/Validator';
197198
import { ValidatorOptions } from '../../src/validation/ValidatorOptions';
@@ -4730,3 +4731,20 @@ describe('IsBase58', () => {
47304731
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
47314732
});
47324733
});
4734+
4735+
describe('IsISO4217', () => {
4736+
class MyClass {
4737+
@IsISO4217CurrencyCode()
4738+
someProperty: string;
4739+
}
4740+
4741+
it('should not fail for a valid ISO4217 code', () => {
4742+
const validValues = ['EUR', 'USD', 'BDT', 'LRD'];
4743+
return checkValidValues(new MyClass(), validValues);
4744+
});
4745+
4746+
it('should fail for invalid values', () => {
4747+
const invalidValues = [undefined, null, '', 'USS'];
4748+
return checkInvalidValues(new MyClass(), invalidValues);
4749+
});
4750+
});

0 commit comments

Comments
 (0)