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
58 lines (47 loc) · 2.12 KB
/
Copy pathvalidators.ts
File metadata and controls
58 lines (47 loc) · 2.12 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
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 < 2)
errors.participants = t('wizard.validation.minParticipants');
const hasUnnamed = value.participants.some((p) => !p.name.trim());
if (hasUnnamed) errors.participants = t('wizard.validation.participantNameRequired');
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 = t('wizard.validation.minOneItem');
const hasUnnamed = value.items.some((i) => !i.name.trim());
if (hasUnnamed) errors.items = t('wizard.validation.itemNameRequired');
const hasZeroPrice = value.items.some((i) => i.price <= 0);
if (hasZeroPrice) errors.items = t('wizard.validation.itemPriceRequired');
return errors;
};