forked from typestack/class-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
@IsISO4217CurrencyCode
decorator (typestack#1824)
- Loading branch information
1 parent
b564f8d
commit 7fe37ed
Showing
4 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters