-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathValidationSchema.tsx
More file actions
174 lines (168 loc) · 6.37 KB
/
Copy pathValidationSchema.tsx
File metadata and controls
174 lines (168 loc) · 6.37 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import * as Yup from 'yup';
import { IsPartOfCulturalRoute } from './constants';
import { VALIDATION_MESSAGE_KEYS } from '../../app/forms/constants';
type EnrolmentFormValidationSchemaProps = {
minGroupSize?: number;
maxGroupSize?: number;
isQueueEnrolment: boolean;
};
export default function getValidationSchema({
maxGroupSize,
minGroupSize,
isQueueEnrolment,
}: EnrolmentFormValidationSchemaProps) {
return Yup.object().shape({
isPartOfCulturalRoute: Yup.string()
.oneOf(
[IsPartOfCulturalRoute.NO_OR_UNKNOWN, IsPartOfCulturalRoute.YES],
VALIDATION_MESSAGE_KEYS.STRING_REQUIRED
)
.required(VALIDATION_MESSAGE_KEYS.STRING_REQUIRED),
hasEmailNotification: Yup.bool().oneOf(
[true],
'enrolment:enrolmentForm.validation.hasEmailNotification'
),
isSharingDataAccepted: Yup.bool().oneOf(
[true],
'enrolment:enrolmentForm.validation.isSharingDataAccepted'
),
language: Yup.string().required(VALIDATION_MESSAGE_KEYS.STRING_REQUIRED),
person: Yup.object().when(
['isSameResponsiblePerson'],
([isSameResponsiblePerson], schema: Yup.AnyObjectSchema) => {
return isSameResponsiblePerson
? schema
: schema.shape({
name: Yup.string().required(
VALIDATION_MESSAGE_KEYS.STRING_REQUIRED
),
emailAddress: Yup.string()
.required(VALIDATION_MESSAGE_KEYS.STRING_REQUIRED)
.email(VALIDATION_MESSAGE_KEYS.EMAIL),
});
}
),
studyGroup: Yup.object().when(
['isMandatoryAdditionalInformationRequired'],
(
[isMandatoryAdditionalInformationRequired],
schema: Yup.AnyObjectSchema
) => {
const validateSumOfSizePair = (
sizePair: number | undefined,
schema: Yup.NumberSchema,
totalMinLimitValidationMessage: string,
fieldMaxLimitValidationMessage: string,
totalMaxLimitValidationMessage: string
): Yup.NumberSchema => {
// Validate a single field against the total min and max sizes.
// The used validation error message will be the same for both the fields.
// This is also preventing negative param.max to occur in validation.
if (
minGroupSize &&
maxGroupSize &&
(!sizePair ||
sizePair < 0 ||
sizePair < minGroupSize ||
sizePair > maxGroupSize)
) {
return (
schema
// Min-limit is current a gap to minimum group size.
.min(
sizePair != null ? minGroupSize - sizePair : minGroupSize,
() => ({
min: minGroupSize,
key: totalMinLimitValidationMessage,
})
)
// Max-limit is maximum group size
.max(maxGroupSize, (param) => ({
max: param.max,
key: totalMaxLimitValidationMessage,
}))
);
}
if (maxGroupSize && sizePair) {
// After the field pair is given, count how many seats are left
// and use that as max limit.
// The used validation error message will be unique for both the fields.
return schema.max(maxGroupSize - sizePair, (param) => ({
max: param.max,
key: fieldMaxLimitValidationMessage,
}));
}
return schema;
};
return schema.shape(
{
person: Yup.object().shape({
name: Yup.string().required(
VALIDATION_MESSAGE_KEYS.STRING_REQUIRED
),
phoneNumber: Yup.string().required(
VALIDATION_MESSAGE_KEYS.STRING_REQUIRED
),
emailAddress: Yup.string()
.required(VALIDATION_MESSAGE_KEYS.STRING_REQUIRED)
.email(VALIDATION_MESSAGE_KEYS.EMAIL),
}),
unitName: Yup.string().when(['unitId'], ([unitId], schema) => {
if (!unitId) {
return schema.required(VALIDATION_MESSAGE_KEYS.STRING_REQUIRED);
}
return schema;
}),
unitId: Yup.string().when(['unitName'], ([unitName], schema) => {
if (!unitName) {
return schema
.nullable()
.required(VALIDATION_MESSAGE_KEYS.STRING_REQUIRED);
}
return schema.default(null).nullable();
}),
groupName: Yup.string().required(
VALIDATION_MESSAGE_KEYS.STRING_REQUIRED
),
// NOTE: GroupSize is (currently) the amount of children
groupSize: Yup.number()
.required(VALIDATION_MESSAGE_KEYS.NUMBER_REQUIRED)
.when(['amountOfAdult'], ([sizePair], schema) =>
validateSumOfSizePair(
sizePair,
schema,
VALIDATION_MESSAGE_KEYS.STUDYGROUP_MIN_CHILDREN_WITH_ADULTS,
VALIDATION_MESSAGE_KEYS.STUDYGROUP_MAX_WITH_ADULTS,
VALIDATION_MESSAGE_KEYS.STUDYGROUP_MAX_CHILDREN_WITH_ADULTS
)
),
amountOfAdult: Yup.number()
.required(VALIDATION_MESSAGE_KEYS.NUMBER_REQUIRED)
.when(['groupSize'], ([sizePair], schema) =>
validateSumOfSizePair(
sizePair,
schema,
VALIDATION_MESSAGE_KEYS.STUDYGROUP_MIN_CHILDREN_WITH_ADULTS,
VALIDATION_MESSAGE_KEYS.STUDYGROUP_MAX_WITH_CHILDREN,
VALIDATION_MESSAGE_KEYS.STUDYGROUP_MAX_CHILDREN_WITH_ADULTS
)
),
extraNeeds: isMandatoryAdditionalInformationRequired
? Yup.string().required(VALIDATION_MESSAGE_KEYS.STRING_REQUIRED)
: Yup.string(),
preferredTimes: isQueueEnrolment
? Yup.string().required(VALIDATION_MESSAGE_KEYS.STRING_REQUIRED)
: Yup.string(),
studyLevels: Yup.array()
.required(VALIDATION_MESSAGE_KEYS.STRING_REQUIRED)
.min(1, VALIDATION_MESSAGE_KEYS.STRING_REQUIRED),
},
[
['groupSize', 'amountOfAdult'],
['unitName', 'unitId'],
]
);
}
),
});
}