Feature/support derived currencies in currency validation (#3529)
* Support derived currencies in currency validation * Update changelogpull/3526/head^2
parent
8386fec98a
commit
f08b0b570b
@ -0,0 +1,44 @@
|
||||
import { DERIVED_CURRENCIES } from '@ghostfolio/common/config';
|
||||
|
||||
import {
|
||||
registerDecorator,
|
||||
ValidationOptions,
|
||||
ValidatorConstraint,
|
||||
ValidatorConstraintInterface,
|
||||
ValidationArguments
|
||||
} from 'class-validator';
|
||||
import { isISO4217CurrencyCode } from 'class-validator';
|
||||
|
||||
export function IsCurrencyCode(validationOptions?: ValidationOptions) {
|
||||
return function (object: Object, propertyName: string) {
|
||||
registerDecorator({
|
||||
propertyName,
|
||||
constraints: [],
|
||||
options: validationOptions,
|
||||
target: object.constructor,
|
||||
validator: IsExtendedCurrencyConstraint
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
@ValidatorConstraint({ async: false })
|
||||
export class IsExtendedCurrencyConstraint
|
||||
implements ValidatorConstraintInterface
|
||||
{
|
||||
public defaultMessage(args: ValidationArguments) {
|
||||
return '$value 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)
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
{
|
||||
"meta": {
|
||||
"date": "2024-06-28T00:00:00.000Z",
|
||||
"version": "dev"
|
||||
},
|
||||
"accounts": [
|
||||
{
|
||||
"balance": 2000,
|
||||
"currency": "USD",
|
||||
"id": "b2d3fe1d-d6a8-41a3-be39-07ef5e9480f0",
|
||||
"isExcluded": false,
|
||||
"name": "My Online Trading Account",
|
||||
"platformId": null
|
||||
}
|
||||
],
|
||||
"activities": [
|
||||
{
|
||||
"accountId": "b2d3fe1d-d6a8-41a3-be39-07ef5e9480f0",
|
||||
"comment": null,
|
||||
"fee": 0,
|
||||
"quantity": 5,
|
||||
"type": "BUY",
|
||||
"unitPrice": 10875.00,
|
||||
"currency": "ZAc",
|
||||
"dataSource": "YAHOO",
|
||||
"date": "2024-06-27T22:00:00.000Z",
|
||||
"symbol": "JSE.JO"
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in new issue