-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationService.js
More file actions
370 lines (331 loc) · 12.2 KB
/
ValidationService.js
File metadata and controls
370 lines (331 loc) · 12.2 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
if (!window.SequraFE) {
window.SequraFE = {};
}
if (typeof SequraFE.regex === 'undefined'){
throw new Error('SequraFE.regex is not defined. Please provide the regex definitions before loading the ValidationService.');
}
(function () {
/**
* @typedef ValidationMessage
* @property {string} code The message code.
* @property {string} field The field name that the error is related to.
* @property {string} message The error message.
*/
/**
* @typedef CategoryPaymentMethod
* @property {string|null} category
* @property {string|null} product
* @property {string|null} title
*/
const validationRule = {
numeric: 'numeric',
integer: 'integer',
required: 'required',
greaterThanZero: 'greaterThanZero',
minValue: 'minValue',
maxValue: 'maxValue',
nonNegative: 'nonNegative',
greaterThanX: 'greaterThanX'
};
const { templateService, utilities, translationService } = SequraFE;
/**
* Validates if the input has a value. If the value is not set, adds an error class to the input element.
*
* @param {HTMLInputElement|HTMLSelectElement} input
* @param {string?} message
* @return {boolean}
*/
const validateRequiredField = (input, message) => {
return validateField(input, !input.value?.trim() || (input.type === 'checkbox' && !input.checked), message);
};
/**
* Validates a numeric input.
*
* @param {HTMLInputElement} input
* @param {string?} message
* @return {boolean} Indication of the validity.
*/
const validateNumber = (input, message) => {
const ruleset = input.dataset?.validationRule ? input.dataset.validationRule.split(',') : [];
let result = true;
if (!validateField(input, Number.isNaN(input.value), message)) {
return false;
}
const value = Number(input.value);
ruleset.forEach((rule) => {
if (!result) {
// break on first false rule
return;
}
let condition = false;
let subValue = null;
if (rule.includes('|')) {
[rule, subValue] = rule.split('|');
}
// condition should be positive for valid values
switch (rule) {
case validationRule.integer:
condition = Number.isInteger(value);
break;
case validationRule.greaterThanZero:
condition = value > 0;
break;
case validationRule.minValue:
condition = value >= Number(subValue);
break;
case validationRule.maxValue:
condition = value <= Number(subValue);
break;
case validationRule.nonNegative:
condition = value >= 0;
break;
case validationRule.required:
condition = !!input.value?.trim();
break;
case validationRule.greaterThanX:
condition = value >= Number(document.querySelector(`input[name="${subValue}"]`)?.value);
break;
default:
return;
}
if (!validateField(input, !condition, message)) {
result = false;
}
});
return result;
};
/**
* Validates if the input is a valid email. If not, adds the error class to the input element.
*
* @param {HTMLInputElement} input
* @param {string?} message
* @return {boolean}
*/
const validateEmail = (input, message) => {
const regex = new RegExp(SequraFE.regex.email);
return validateField(input, !regex.test(String(input.value).toLowerCase()), message);
};
/**
* Validates if the input is a valid URL. If not, adds an error class to the input element.
*
* @param {HTMLInputElement} input
* @param {string?} message
* @return {boolean}
*/
const validateUrl = (input, message) => {
const regex = new RegExp(SequraFE.regex.url);
return validateField(input, !regex.test(String(input.value).toLowerCase()), message);
};
/**
* Validates if the input field is longer than a specified number of characters.
* If so, adds an error class to the input element.
*
* @param {HTMLInputElement} input
* @param {string?} message
* @return {boolean}
*/
const validateMaxLength = (input, message) => {
return validateField(input, input.dataset.maxLength && input.value.length > input.dataset.maxLength, message);
};
/**
* Handles validation errors. These errors come from the back end.
*
* @param {ValidationMessage[]} errors
*/
const handleValidationErrors = (errors) => {
for (const error of errors) {
markFieldGroupInvalid(`[name=${error.field}]`, error.message);
}
};
/**
* Marks a field as invalid.
*
* @param {string} fieldSelector The field selector.
* @param {string} message The message to display.
* @param {Element} [parent] A parent element.
*/
const markFieldGroupInvalid = (fieldSelector, message, parent) => {
if (!parent) {
parent = templateService.getMainPage();
}
const inputEl = parent.querySelector(fieldSelector);
inputEl && setError(inputEl, message);
};
/**
* Sets error for an input.
*
* @param {HTMLElement} element
* @param {string?} message
*/
const setError = (element, message) => {
const parent = utilities.getAncestor(element, 'sq-field-wrapper');
parent && parent.classList.add('sqs--error');
if (message) {
let errorField = parent.querySelector('.sqp-input-error');
if (!errorField) {
errorField = SequraFE.elementGenerator.createElement('span', 'sqp-input-error', message);
parent.append(errorField);
}
errorField.innerHTML = translationService.translate(message);
}
};
/**
* Removes error from input form group element.
*
* @param {HTMLElement} element
*/
const removeError = (element) => {
const parent = utilities.getAncestor(element, 'sq-field-wrapper');
parent && parent.classList.remove('sqs--error');
};
/**
* Validates if the input is a valid css selector. If not, adds the error class to the input element.
*
* @param {HTMLInputElement} input
* @param {boolean} required
* @param {string?} message
* @return {boolean}
*/
const validateCssSelector = (input, required, message) => {
let isValid = false;
try {
document.querySelector(input.value);
isValid = true;
} catch {
isValid = !required && !input.value;
}
return validateField(input, !isValid, message);
};
/**
* Validates if the value is a valid date or duration following ISO 8601 format.
*
* @param {string} str
* @return {boolean}
*/
const validateDateOrDuration = (str) => {
const regex = new RegExp(SequraFE.regex.dateOrDuration);
return regex.test(str) && 'P' !== str && !str.endsWith('T');
};
/**
* Check if a given string is a valid IP address.
*
* @param {string} str
*
* @returns {boolean}
*/
const validateIpAddress = (str) => {
const regex = new RegExp(SequraFE.regex.ip);
return regex.test(str);
};
/**
* Validates the provided JSON string and marks field invalid if the JSON is invalid.
*
* @param {HTMLElement} element
* @param {boolean} required If true, the field is required.
* @param {string?} message
* @return {boolean}
*/
const validateJSON = (element, required, message) => {
let isValid = false;
try {
JSON.parse(element.value);
isValid = true;
} catch (e) {
isValid = !required && !element.value;
}
return validateField(element, !isValid, message);
};
/**
* Validates the condition against the input field and marks field invalid if the error condition is met.
*
* @param {HTMLElement} element
* @param {boolean} errorCondition Error condition.
* @param {string?} message
* @return {boolean}
*/
const validateField = (element, errorCondition, message) => {
removeError(element);
if (errorCondition) {
setError(element, message);
return false;
}
return true;
};
/**
* Validates custom locations.
* @param {Array<HTMLElement>} element Each element in the array should be the details element containing the custom location data.
* @param {Array<Object>} value
* @param {string} value[].selForTarget CSS selector for the target element.
* @param {string} value[].widgetStyles JSON string representing the styles for the widget.
* @param {string} value[].product Product name.
* @param {CategoryPaymentMethod[]} allowedPaymentMethods Array of allowed payment methods.
* @return {boolean}
*/
const validateCustomLocations = (element, value, allowedPaymentMethods) => {
let isValid = true;
for (let i = 0; i < element.length; i++) {
const location = value[i];
const detailsElement = element[i];
isValid = validateCssSelector(
detailsElement.querySelector('input[type="text"]'),
false,
'validation.invalidField'
) && isValid;
isValid = validateJSON(
detailsElement.querySelector('textarea'),
false,
'validation.invalidJSON'
) && isValid;
let isPaymentMethodValid = allowedPaymentMethods.some(pm => pm.product === location.product)
&& value.filter(l => l.product === location.product).length === 1;
isValid = validateField(
detailsElement.querySelector('select'),
!isPaymentMethodValid,
'validation.invalidField'
) && isValid;
}
return isValid;
}
/**
* Validates related fields and disables the footer if any of them is invalid.
* @param {string} parentField The parent field name that controls the visibility of related fields.
* @param {Array<Object>} fieldsRelationships An array of objects containing the relationships between fields.
* @param {string} fieldsRelationships[].parentField The parent field name that controls the visibility of related fields.
* @param {Array<string>} fieldsRelationships[].requiredFields An array of field names that are required when the parent field is shown.
* @param {Array<string>} fieldsRelationships[].fields An array of field names that are related to the parent field.
* @param {boolean} show Whether to show or hide the related fields.
* @return {boolean} Returns true if all related fields are valid, false otherwise.
*/
const validateRelatedFields = (parentField, fieldsRelationships, show) => {
if (!show) {
return true;
}
let isValid = true;
const { requiredFields, fields } = fieldsRelationships.find(group => group.parentField === parentField) || { requiredFields: [], fields: [] };
for (let i = 0; i < fields.length; i++) {
isValid = validateCssSelector(
document.querySelector(`[name="${fields[i]}"]`),
requiredFields.includes(fields[i]),
'validation.invalidField'
) && isValid;
}
return isValid;
}
SequraFE.validationService = {
setError,
removeError,
validateEmail,
validateNumber,
validateUrl,
validateMaxLength,
validateCssSelector,
validateJSON,
validateRelatedFields,
validateCustomLocations,
validateField,
validateRequiredField,
validateDateOrDuration,
validateIpAddress,
handleValidationErrors
};
})();