Skip to content

Commit 698f557

Browse files
committed
lp-1013 - capital contribution validation
- add function validator - add tests
1 parent 945bd39 commit 698f557

File tree

6 files changed

+1286
-1
lines changed

6 files changed

+1286
-1
lines changed

locales/cy/errors.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"errorMessages": {
3+
"capitalContribution": {
4+
"twoDecimalPlaces": "WELSH - Value must be a number with 2 decimal places",
5+
"moreThanZero": "WELSH - Value must be 0.01 or more",
6+
"noSymbols": "WELSH - Value must not include currency symbols like £, $ and €",
7+
"noComma": "WELSH - Value must not include a comma",
8+
"atLeastOneType": "WELSH - Select at least one type of contribution",
9+
"currencyRequired": "WELSH - Select the currency of the capital contribution"
10+
}
11+
}
12+
}

locales/en/errors.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"errorMessages": {
3+
"capitalContribution": {
4+
"twoDecimalPlaces": "Value must be a number with 2 decimal places",
5+
"moreThanZero": "Value must be 0.01 or more",
6+
"noSymbols": "Value must not include currency symbols like £, $ and €",
7+
"noComma": "Value must not include a comma",
8+
"atLeastOneType": "Select at least one type of contribution",
9+
"currencyRequired": "Select the currency of the capital contribution"
10+
}
11+
}
12+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import UIErrors from "../../../domain/entities/UIErrors";
2+
import { symbols } from "./currencies";
3+
4+
const capitalContributionValidation = (
5+
data: {
6+
contribution_currency_type: string;
7+
contribution_currency_value: string;
8+
contribution_sub_types: string[];
9+
},
10+
i18n: any
11+
): void => {
12+
const uiErrors = new UIErrors();
13+
14+
contributionCurrencyValueValidation(data, uiErrors, i18n);
15+
16+
if (!data.contribution_sub_types.length) {
17+
uiErrors.formatValidationErrorToUiErrors({
18+
errors: {
19+
contribution_sub_types: i18n?.errorMessages?.capitalContribution?.atLeastOneType
20+
}
21+
});
22+
}
23+
24+
if (!data.contribution_currency_type) {
25+
uiErrors.formatValidationErrorToUiErrors({
26+
errors: {
27+
contribution_currency_type: i18n?.errorMessages?.capitalContribution?.currencyRequired
28+
}
29+
});
30+
}
31+
32+
if (uiErrors.hasErrors()) {
33+
throw uiErrors;
34+
}
35+
};
36+
37+
const contributionCurrencyValueValidation = (
38+
data: { contribution_currency_type: string; contribution_currency_value: string; contribution_sub_types: string[] },
39+
uiErrors: UIErrors,
40+
i18n: any
41+
) => {
42+
if (hasSymbol(data.contribution_currency_value, symbols)) {
43+
uiErrors.formatValidationErrorToUiErrors({
44+
errors: {
45+
contribution_currency_value: i18n?.errorMessages?.capitalContribution?.noSymbols
46+
}
47+
});
48+
} else if (hasComma(data.contribution_currency_value)) {
49+
uiErrors.formatValidationErrorToUiErrors({
50+
errors: {
51+
contribution_currency_value: i18n?.errorMessages?.capitalContribution?.noComma
52+
}
53+
});
54+
} else if (!isNumber(data.contribution_currency_value) || !has2Decimal(data.contribution_currency_value)) {
55+
uiErrors.formatValidationErrorToUiErrors({
56+
errors: {
57+
contribution_currency_value: i18n?.errorMessages?.capitalContribution?.twoDecimalPlaces
58+
}
59+
});
60+
} else if (!isGreaterThanZero(data.contribution_currency_value)) {
61+
uiErrors.formatValidationErrorToUiErrors({
62+
errors: {
63+
contribution_currency_value: i18n?.errorMessages?.capitalContribution?.moreThanZero
64+
}
65+
});
66+
}
67+
};
68+
69+
const isNumber = (value: string) => {
70+
return !isNaN(Number(value));
71+
};
72+
73+
const has2Decimal = (value: string) => {
74+
return value.split(".")[1]?.length === 2;
75+
};
76+
77+
const isGreaterThanZero = (value: string) => {
78+
return parseFloat(value) > 0;
79+
};
80+
81+
const hasSymbol = (str: string, symbols: string[]): boolean => {
82+
return symbols.some((symbol) => str.includes(symbol));
83+
};
84+
85+
const hasComma = (str: string): boolean => {
86+
return str.includes(",");
87+
};
88+
89+
export { capitalContributionValidation };

0 commit comments

Comments
 (0)