forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.ts
More file actions
64 lines (51 loc) · 2.47 KB
/
Copy pathvalidators.ts
File metadata and controls
64 lines (51 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import type { WizardState } from '../../types/wizard';
type Errors = Record<string, string>;
export const validateBasicInfo = (
value: Pick<WizardState, 'title' | 'currency' | 'totalAmount'>,
t: (key: string) => string
): Errors => {
const errors: Errors = {};
if (!value.title.trim()) errors.title = t('wizard.validation.titleRequired');
if (!value.currency) errors.currency = t('wizard.validation.currencyRequired');
if (!value.totalAmount || value.totalAmount <= 0)
errors.totalAmount = t('wizard.validation.totalAmountRequired');
return errors;
};
export const validateParticipants = (
value: Pick<WizardState, 'participants' | 'splitMethod' | 'totalAmount'>,
t: (key: string) => string
): Errors => {
const errors: Errors = {};
if (value.participants.length < 1)
errors.participants = 'Add at least one participant';
const hasUnnamed = value.participants.some((p) => !p.name.trim());
if (hasUnnamed) errors.participants = t('wizard.validation.participantNameRequired');
const hasValidWallet = value.participants.some((p) => p.walletAddress && p.walletAddress.trim().length > 0);
if (!hasValidWallet) errors.participants = 'Add at least one participant with a valid wallet address';
if (value.splitMethod === 'percentage') {
const total = value.participants.reduce((acc, p) => acc + (p.percentage ?? 0), 0);
if (Math.abs(total - 100) > 0.01)
errors.participants = t('wizard.validation.percentageMustEqual100');
}
if (value.splitMethod === 'custom') {
const total = value.participants.reduce((acc, p) => acc + (p.customAmount ?? 0), 0);
if (Math.abs(total - value.totalAmount) > 0.01)
errors.participants = t('wizard.validation.customMustEqualTotal');
}
return errors;
};
export const validateItems = (
value: Pick<WizardState, 'items'>,
t: (key: string) => string
): Errors => {
const errors: Errors = {};
if (value.items.length === 0)
errors.items = 'Add at least one item';
const hasUnnamed = value.items.some((i) => !i.name.trim());
if (hasUnnamed) errors.items = t('wizard.validation.itemNameRequired');
const hasPositiveAmount = value.items.some((i) => i.price > 0);
if (!hasPositiveAmount) errors.items = 'Add at least one item with a positive amount';
const hasZeroPrice = value.items.some((i) => i.price <= 0);
if (hasZeroPrice) errors.items = t('wizard.validation.itemPriceRequired');
return errors;
};