From 3b59d7989a23c9dd6c59c12b604cb8ad0d6c801b Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Fri, 25 Apr 2025 08:24:16 +0200 Subject: [PATCH] Feature/improve currency code validation (#4598) * Improve currency code validation * Update changelog --- CHANGELOG.md | 5 +++++ apps/api/src/validators/is-currency-code.ts | 21 +++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db8312af..328d7749 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Changed + +- Improved the error message of the currency code validation +- Tightened the currency code validation by requiring uppercase letters + ### Fixed - Improved the file selector of the activities import functionality to accept case-insensitive file extensions (`.CSV` and `.JSON`) diff --git a/apps/api/src/validators/is-currency-code.ts b/apps/api/src/validators/is-currency-code.ts index 34a82c48..d04da780 100644 --- a/apps/api/src/validators/is-currency-code.ts +++ b/apps/api/src/validators/is-currency-code.ts @@ -25,19 +25,24 @@ export class IsExtendedCurrencyConstraint implements ValidatorConstraintInterface { public defaultMessage() { - return '$value must be a valid ISO4217 currency code'; + return '$property must be a valid ISO4217 currency code'; } public validate(currency: any) { // Return true if currency is a standard ISO 4217 code or a derived currency return ( - isISO4217CurrencyCode(currency) || - [ - ...DERIVED_CURRENCIES.map((derivedCurrency) => { - return derivedCurrency.currency; - }), - 'USX' - ].includes(currency) + this.isUpperCase(currency) && + (isISO4217CurrencyCode(currency) || + [ + ...DERIVED_CURRENCIES.map((derivedCurrency) => { + return derivedCurrency.currency; + }), + 'USX' + ].includes(currency)) ); } + + private isUpperCase(aString: string) { + return aString === aString?.toUpperCase(); + } }