-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcollector.utils.ts
More file actions
787 lines (743 loc) · 27.1 KB
/
collector.utils.ts
File metadata and controls
787 lines (743 loc) · 27.1 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/*
* Copyright (c) 2025 Ping Identity Corporation. All rights reserved.
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
/**
* Import the required types
*/
import type {
ActionCollectors,
ActionCollectorTypes,
InferSingleValueCollectorType,
InferMultiValueCollectorType,
SingleValueCollectorTypes,
MultiValueCollectorTypes,
InferActionCollectorType,
NoValueCollectorTypes,
InferNoValueCollectorType,
ValidatedSingleValueCollectorWithValue,
ValidatedTextCollector,
InferValueObjectCollectorType,
ObjectValueCollectorTypes,
UnknownCollector,
InferAutoCollectorType,
PhoneNumberOutputValue,
MultiValueCollectors,
ObjectValueCollectors,
AutoCollectors,
SingleValueAutoCollectorTypes,
ObjectValueAutoCollectorTypes,
QrCodeCollectorBase,
} from './collector.types.js';
import type {
DeviceAuthenticationField,
DeviceRegistrationField,
FidoAuthenticationField,
FidoRegistrationField,
MultiSelectField,
PhoneNumberField,
ProtectField,
QrCodeField,
ReadOnlyField,
RedirectField,
SingleSelectField,
StandardField,
ValidatedField,
} from './davinci.types.js';
/**
* @function returnActionCollector - Creates an ActionCollector object based on the provided field and index.
* @param {DaVinciField} field - The field object containing key, label, type, and links.
* @param {number} idx - The index to be used in the id of the ActionCollector.
* @param {ActionCollectorTypes} [collectorType] - Optional type of the ActionCollector.
* @returns {ActionCollector} The constructed ActionCollector object.
*/
export function returnActionCollector<CollectorType extends ActionCollectorTypes>(
field: RedirectField | StandardField,
idx: number,
collectorType: CollectorType,
): ActionCollectors {
let error = '';
if (!('label' in field)) {
error = `${error}Label is not found in the field object. `;
}
if (!('type' in field)) {
error = `${error}Type is not found in the field object. `;
}
if (collectorType === 'IdpCollector') {
let link;
if (!('links' in field)) {
error = `${error}Links property is not found in the field object. `;
link = null;
} else {
link = field.links?.['authenticate']?.href || null;
}
return {
category: 'ActionCollector',
error: error || null,
type: collectorType,
id: `${field.key || field.type}-${idx}`,
name: field.key || field.type,
output: {
key: `${field.key || field.type}-${idx}`,
label: field.label,
type: field.type,
url: link,
},
} as InferActionCollectorType<'IdpCollector'>;
} else {
if (!('key' in field)) {
error = `${error}Key is not found in the field object. `;
}
return {
category: 'ActionCollector',
error: error || null,
type: collectorType || 'ActionCollector',
id: `${field.key}-${idx}`,
name: field.key,
output: {
key: field.key,
label: field.label,
type: field.type,
},
} as InferActionCollectorType<CollectorType>;
}
}
/**
* @function returnFlowCollector - Returns a flow collector object
* @param {DaVinciField} field - The field representing the flow button
* @param {number} idx - The index of the field in the form
* @returns {FlowCollector} - The flow collector object
*/
export function returnFlowCollector(field: StandardField, idx: number) {
return returnActionCollector(field, idx, 'FlowCollector');
}
/**
* @function returnIdpCollector - Returns a social login collector object
* @param {DaVinciField} field - The field representing the social login button
* @param {number} idx - The index of the field in the form
* @returns {SocialLoginCollector} - The social login collector object
*/
export function returnIdpCollector(field: RedirectField, idx: number) {
return returnActionCollector(field, idx, 'IdpCollector');
}
/**
* @function returnSubmitCollector - Returns a submit collector object
* @param {DaVinciField} field - The field representing the submit button
* @param {number} idx - The index of the field in the form
* @returns {ActionCollector} - The submit collector object
*/
export function returnSubmitCollector(field: StandardField, idx: number) {
return returnActionCollector(field, idx, 'SubmitCollector');
}
/**
* @function returnSingleValueCollector - Creates a SingleValueCollector object based on the provided field, index, and optional collector type.
* @param {DaVinciField} field - The field object containing key, label, type, and links.
* @param {number} idx - The index to be used in the id of the SingleValueCollector.
* @param {SingleValueCollectorTypes} [collectorType] - Optional type of the SingleValueCollector.
* @returns {SingleValueCollector} The constructed SingleValueCollector object.
*/
export function returnSingleValueCollector<
Field extends StandardField | SingleSelectField | ValidatedField,
CollectorType extends SingleValueCollectorTypes = 'SingleValueCollector',
>(field: Field, idx: number, collectorType: CollectorType, data?: string) {
let error = '';
if (!('key' in field)) {
error = `${error}Key is not found in the field object. `;
}
if (!('label' in field)) {
error = `${error}Label is not found in the field object. `;
}
if (!('type' in field)) {
error = `${error}Type is not found in the field object. `;
}
if (collectorType === 'PasswordCollector') {
return {
category: 'SingleValueCollector',
error: error || null,
type: collectorType,
id: `${field?.key}-${idx}`,
name: field.key,
input: {
key: field.key,
value: '',
type: field.type,
},
output: {
key: field.key,
label: field.label,
type: field.type,
// No default or existing value is passed
},
} as InferSingleValueCollectorType<CollectorType>;
} else if (collectorType === 'SingleSelectCollector') {
/**
* Check if options are present in the field object first
* If found, return existing error, which should be ''
* If not found, add additional message to error string
*/
const err = 'options' in field ? error : `${error}Options are not found in the field object. `;
const options = 'options' in field ? field.options : []; // Fallback to ensure type consistency
return {
category: 'SingleValueCollector',
error: err || null,
type: collectorType,
id: `${field.key}-${idx}`,
name: field.key,
input: {
key: field.key,
value: '',
type: field.type,
},
output: {
key: field.key,
label: field.label,
type: field.type,
value: data || '',
options: options,
},
} as InferSingleValueCollectorType<'SingleSelectCollector'>;
} else if ('validation' in field || 'required' in field) {
const validationArray = [];
if ('validation' in field) {
validationArray.push({
type: 'regex',
message: field.validation?.errorMessage || '',
rule: field.validation?.regex || '',
});
}
if ('required' in field && field.required === true) {
validationArray.push({
type: 'required',
message: 'Value cannot be empty',
rule: true,
});
}
return {
category: 'ValidatedSingleValueCollector',
error: error || null,
type: 'TextCollector',
id: `${field.key}-${idx}`,
name: field.key,
input: {
key: field.key,
value: data || '',
type: field.type,
validation: validationArray,
},
output: {
key: field.key,
label: field.label,
type: field.type,
value: data || '',
},
} as ValidatedSingleValueCollectorWithValue<'TextCollector'>;
} else {
return {
category: 'SingleValueCollector',
error: error || null,
type: collectorType || 'SingleValueCollector',
id: `${field.key}-${idx}`,
name: field.key,
input: {
key: field.key,
value: '',
type: field.type,
},
output: {
key: field.key,
label: field.label,
type: field.type,
value: data || '',
},
} as InferSingleValueCollectorType<CollectorType>;
}
}
/**
* @function returnSingleValueAutoCollector - Creates a SingleValueAutoCollector object based on the provided field, index, and optional collector type.
* @param {DaVinciField} field - The field object containing key, label, type, and links.
* @param {number} idx - The index to be used in the id of the AutoCollector.
* @param {SingleValueAutoCollectorTypes} [collectorType] - Optional type of the AutoCollector.
* @returns {AutoCollector} The constructed AutoCollector object.
*/
export function returnSingleValueAutoCollector<
Field extends ProtectField,
CollectorType extends SingleValueAutoCollectorTypes = 'SingleValueAutoCollector',
>(field: Field, idx: number, collectorType: CollectorType) {
let error = '';
if (!('key' in field)) {
error = `${error}Key is not found in the field object. `;
}
if (!('type' in field)) {
error = `${error}Type is not found in the field object. `;
}
if (collectorType === 'ProtectCollector') {
return {
category: 'SingleValueAutoCollector',
error: error || null,
type: collectorType,
id: `${field?.key}-${idx}`,
name: field.key,
input: {
key: field.key,
value: '',
type: field.type,
},
output: {
key: field.key,
type: field.type,
config: {
behavioralDataCollection: field.behavioralDataCollection,
universalDeviceIdentification: field.universalDeviceIdentification,
},
},
} as InferAutoCollectorType<'ProtectCollector'>;
} else {
return {
category: 'SingleValueAutoCollector',
error: error || null,
type: collectorType || 'SingleValueAutoCollector',
id: `${field.key}-${idx}`,
name: field.key,
input: {
key: field.key,
value: '',
type: field.type,
},
output: {
key: field.key,
type: field.type,
},
} as InferAutoCollectorType<CollectorType>;
}
}
/**
* @function returnObjectValueAutoCollector - Creates an ObjectValueAutoCollector object based on the provided field, index, and optional collector type.
* @param {DaVinciField} field - The field object containing key, label, type, and links.
* @param {number} idx - The index to be used in the id of the AutoCollector.
* @param {ObjectValueAutoCollectorTypes} [collectorType] - Optional type of the AutoCollector.
* @returns {AutoCollector} The constructed AutoCollector object.
*/
export function returnObjectValueAutoCollector<
Field extends FidoRegistrationField | FidoAuthenticationField,
CollectorType extends ObjectValueAutoCollectorTypes = 'ObjectValueAutoCollector',
>(field: Field, idx: number, collectorType: CollectorType) {
let error = '';
if (!('key' in field)) {
error = `${error}Key is not found in the field object. `;
}
if (!('type' in field)) {
error = `${error}Type is not found in the field object. `;
}
const validationArray = [];
if ('required' in field && field.required === true) {
validationArray.push({
type: 'required',
message: 'Value cannot be empty',
rule: true,
});
}
if (field.action === 'REGISTER') {
return {
category: 'ObjectValueAutoCollector',
error: error || null,
type: collectorType,
id: `${field?.key}-${idx}`,
name: field.key,
input: {
key: field.key,
value: {},
type: field.type,
validation: validationArray.length ? validationArray : null,
},
output: {
key: field.key,
type: field.type,
config: {
publicKeyCredentialCreationOptions: field.publicKeyCredentialCreationOptions,
action: field.action,
trigger: field.trigger,
},
},
} as InferAutoCollectorType<'FidoRegistrationCollector'>;
} else {
return {
category: 'ObjectValueAutoCollector',
error: error || null,
type: collectorType,
id: `${field?.key}-${idx}`,
name: field.key,
input: {
key: field.key,
value: {},
type: field.type,
validation: validationArray.length ? validationArray : null,
},
output: {
key: field.key,
type: field.type,
config: {
publicKeyCredentialRequestOptions: field.publicKeyCredentialRequestOptions,
action: field.action,
trigger: field.trigger,
},
},
} as InferAutoCollectorType<'FidoAuthenticationCollector'>;
}
}
/**
* @function returnPasswordCollector - Creates a PasswordCollector object based on the provided field and index.
* @param {DaVinciField} field - The field object containing key, label, type, and links.
* @param {number} idx - The index to be used in the id of the PasswordCollector.
* @returns {PasswordCollector} The constructed PasswordCollector object.
*/
export function returnPasswordCollector(field: StandardField, idx: number) {
return returnSingleValueCollector(field, idx, 'PasswordCollector');
}
/**
* @function returnTextCollector - Creates a TextCollector object based on the provided field and index.
* @param {DaVinciField} field - The field object containing key, label, type, and links.
* @param {number} idx - The index to be used in the id of the TextCollector.
* @returns {TextCollector} The constructed TextCollector object.
*/
export function returnTextCollector(field: StandardField, idx: number, data: string) {
return returnSingleValueCollector(field, idx, 'TextCollector', data);
}
/**
* @function returnSingleSelectCollector - Creates a SingleCollector object based on the provided field and index.
* @param {DaVinciField} field - The field object containing key, label, type, and links.
* @param {number} idx - The index to be used in the id of the SingleCollector.
* @returns {SingleValueCollector} The constructed SingleCollector object.
*/
export function returnSingleSelectCollector(field: SingleSelectField, idx: number, data: string) {
return returnSingleValueCollector(field, idx, 'SingleSelectCollector', data);
}
/**
* @function returnProtectCollector - Creates a ProtectCollector object based on the provided field and index.
* @param {DaVinciField} field - The field object containing key, label, type, and links.
* @param {number} idx - The index to be used in the id of the ProtectCollector.
* @returns {ProtectCollector} The constructed ProtectCollector object.
*/
export function returnProtectCollector(field: ProtectField, idx: number) {
return returnSingleValueAutoCollector(field, idx, 'ProtectCollector');
}
/**
* @function returnFidoRegistrationCollector - Creates a FidoRegistrationCollector object based on the provided field and index.
* @param {DaVinciField} field - The field object containing key, label, type, and links.
* @param {number} idx - The index to be used in the id of the FidoRegistrationCollector.
* @returns {FidoRegistrationCollector} The constructed FidoRegistrationCollector object.
*/
export function returnFidoRegistrationCollector(field: FidoRegistrationField, idx: number) {
return returnObjectValueAutoCollector(field, idx, 'FidoRegistrationCollector');
}
/**
* @function returnFidoAuthenticationCollector - Creates a FidoAuthenticationCollector object based on the provided field and index.
* @param {DaVinciField} field - The field object containing key, label, type, and links.
* @param {number} idx - The index to be used in the id of the FidoAuthenticationCollector.
* @returns {FidoAuthenticationCollector} The constructed FidoAuthenticationCollector object.
*/
export function returnFidoAuthenticationCollector(field: FidoAuthenticationField, idx: number) {
return returnObjectValueAutoCollector(field, idx, 'FidoAuthenticationCollector');
}
/**
* @function returnMultiValueCollector - Creates a MultiValueCollector object based on the provided field, index, and optional collector type.
* @param {DaVinciField} field - The field object containing key, label, type, and links.
* @param {number} idx - The index to be used in the id of the MultiValueCollector.
* @param {MultiValueCollectorTypes} [collectorType] - Optional type of the MultiValueCollector.
* @returns {MultiValueCollector} The constructed MultiValueCollector object.
*/
export function returnMultiValueCollector<
Field extends MultiSelectField,
CollectorType extends MultiValueCollectorTypes = 'MultiValueCollector',
>(field: Field, idx: number, collectorType: CollectorType, data?: string[]) {
let error = '';
if (!('key' in field)) {
error = `${error}Key is not found in the field object. `;
}
if (!('label' in field)) {
error = `${error}Label is not found in the field object. `;
}
if (!('type' in field)) {
error = `${error}Type is not found in the field object. `;
}
if (!('options' in field)) {
error = `${error}Options are not found in the field object. `;
}
const validationArray = [];
if ('required' in field && field.required === true) {
validationArray.push({
type: 'required',
message: 'Value cannot be empty',
rule: true,
});
}
return {
category: 'MultiValueCollector',
error: error || null,
type: collectorType || 'MultiValueCollector',
id: `${field.key}-${idx}`,
name: field.key,
input: {
key: field.key,
value: data || [],
type: field.type,
validation: validationArray.length ? validationArray : null,
},
output: {
key: field.key,
label: field.label,
type: field.type,
value: data || [],
options: field.options || [],
},
} as InferMultiValueCollectorType<CollectorType>;
}
/**
* @function returnMultiSelectCollector - Creates a DropDownCollector object based on the provided field and index.
* @param {DaVinciField} field - The field object containing key, label, type, and links.
* @param {number} idx - The index to be used in the id of the DropDownCollector.
* @returns {SingleValueCollector} The constructed DropDownCollector object.
*/
export function returnMultiSelectCollector(field: MultiSelectField, idx: number, data: string[]) {
return returnMultiValueCollector(field, idx, 'MultiSelectCollector', data);
}
/**
* @function returnObjectCollector - Creates a ObjectCollector object based on the provided field, index, and optional collector type.
* @param {DaVinciField} field - The field object containing key, label, type, and links.
* @param {number} idx - The index to be used in the id of the ObjectCollector.
* @param {ObjectValueCollectorTypes} [collectorType] - Optional type of the ObjectCollector.
* @returns {ObjectCollector} The constructed ObjectCollector object.
*/
export function returnObjectCollector<
Field extends DeviceAuthenticationField | DeviceRegistrationField | PhoneNumberField,
CollectorType extends ObjectValueCollectorTypes = 'ObjectValueCollector',
>(field: Field, idx: number, collectorType: CollectorType, prefillData?: PhoneNumberOutputValue) {
let error = '';
if (!('key' in field)) {
error = `${error}Key is not found in the field object. `;
}
if (!('label' in field)) {
error = `${error}Label is not found in the field object. `;
}
if (!('type' in field)) {
error = `${error}Type is not found in the field object. `;
}
const validationArray = [];
if ('required' in field && field.required === true) {
validationArray.push({
type: 'required',
message: 'Value cannot be empty',
rule: true,
});
}
let options;
let defaultValue;
if (field.type === 'DEVICE_AUTHENTICATION') {
if (!('options' in field)) {
error = `${error}Device options are not found in the field object. `;
}
if (Array.isArray(field.options) && field.options.length === 0) {
error = `${error}Device options are not an array or is empty. `;
}
const unmappedDefault = field.options?.find((device) => device.default);
defaultValue = {
type: unmappedDefault ? unmappedDefault.type : '',
value: unmappedDefault ? unmappedDefault.description : '',
id: unmappedDefault ? unmappedDefault.id : '',
};
// Map DaVinci spec to normalized SDK API
options =
field.options?.map((device) => ({
type: device.type,
label: device.title,
content: device.description,
value: device.id,
key: device.id,
default: device.default,
})) ?? [];
} else if (field.type === 'DEVICE_REGISTRATION') {
if (!('options' in field)) {
error = `${error}Device options are not found in the field object. `;
}
if (Array.isArray(field.options) && field.options.length === 0) {
error = `${error}Device options are not an array or is empty. `;
}
defaultValue = '';
// Map DaVinci spec to normalized SDK API
options =
field.options?.map((device, idx) => ({
type: device.type,
label: device.title,
content: device.description,
value: device.type,
key: `${device.type}-${idx}`,
})) ?? [];
} else if (field.type === 'PHONE_NUMBER') {
if ('validatePhoneNumber' in field && field.validatePhoneNumber === true) {
validationArray.push({
type: 'validatePhoneNumber',
message: 'Phone number should be validated',
rule: true,
});
}
const prefilledCountryCode = prefillData?.countryCode;
const prefilledPhone = prefillData?.phoneNumber;
defaultValue = {
countryCode: prefilledCountryCode ? prefilledCountryCode : field.defaultCountryCode || '',
phoneNumber: prefilledPhone || '',
};
}
return {
category: 'ObjectValueCollector',
error: error || null,
type: collectorType || 'ObjectValueCollector',
id: `${field.key}-${idx}`,
name: field.key,
input: {
key: field.key,
value: defaultValue,
type: field.type,
validation: validationArray.length ? validationArray : null,
},
output: {
key: field.key,
label: field.label,
type: field.type,
...(options && { options: options || [] }),
...(defaultValue && { value: defaultValue }),
},
} as InferValueObjectCollectorType<CollectorType>;
}
/**
* @function returnObjectSelectCollector - Creates a DropDownCollector object based on the provided field and index.
* @param {DaVinciField} field - The field object containing key, label, type, and links.
* @param {number} idx - The index to be used in the id of the DropDownCollector.
* @returns {SingleValueCollector} The constructed DropDownCollector object.
*/
export function returnObjectSelectCollector(
field: DeviceAuthenticationField | DeviceRegistrationField,
idx: number,
) {
return returnObjectCollector(
field,
idx,
field.type === 'DEVICE_AUTHENTICATION'
? 'DeviceAuthenticationCollector'
: 'DeviceRegistrationCollector',
);
}
export function returnObjectValueCollector(
field: PhoneNumberField,
idx: number,
prefillData: PhoneNumberOutputValue,
) {
return returnObjectCollector(field, idx, 'PhoneNumberCollector', prefillData);
}
/**
* @function returnNoValueCollector - Creates a NoValueCollector object based on the provided field, index, and optional collector type.
* @param {DaVinciField} field - The field object containing key, label, type, and links.
* @param {number} idx - The index to be used in the id of the NoValueCollector.
* @param {NoValueCollectorTypes} [collectorType] - Optional type of the NoValueCollector.
* @returns {NoValueCollector} The constructed NoValueCollector object.
*/
export function returnNoValueCollector<
Field extends ReadOnlyField | QrCodeField,
CollectorType extends NoValueCollectorTypes = 'NoValueCollector',
>(field: Field, idx: number, collectorType: CollectorType) {
let error = '';
if (!('content' in field)) {
error = `${error}Content is not found in the field object. `;
}
if (!('type' in field)) {
error = `${error}Type is not found in the field object. `;
}
return {
category: 'NoValueCollector',
error: error || null,
type: collectorType || 'NoValueCollector',
id: `${field.key || field.type}-${idx}`,
name: `${field.key || field.type}-${idx}`,
output: {
key: `${field.key || field.type}-${idx}`,
label: field.content,
type: field.type,
},
} as InferNoValueCollectorType<CollectorType>;
}
/**
* @function returnReadOnlyCollector - Creates a ReadOnlyCollector object based on the provided field and index.
* @param {DaVinciField} field - The field object containing key, label, type, and links.
* @param {number} idx - The index to be used in the id of the ReadOnlyCollector.
* @returns {ReadOnlyCollector} The constructed ReadOnlyCollector object.
*/
export function returnReadOnlyCollector(field: ReadOnlyField, idx: number) {
return returnNoValueCollector(field, idx, 'ReadOnlyCollector');
}
/**
* @function returnQrCodeCollector - Creates a QrCodeCollector object for displaying QR code images.
* @param {QrCodeField} field - The field object containing key, content, type, and optional fallbackText.
* @param {number} idx - The index to be used in the id of the QrCodeCollector.
* @returns {QrCodeCollectorBase} The constructed QrCodeCollector object.
*/
export function returnQrCodeCollector(field: QrCodeField, idx: number): QrCodeCollectorBase {
const base = returnNoValueCollector(field, idx, 'QrCodeCollector');
return {
...base,
output: {
...base.output,
label: field.fallbackText || '',
src: field.content || '',
},
};
}
/**
* @function returnValidator - Creates a validator function based on the provided collector
* @param {ValidatedTextCollector | ObjectValueCollectors | MultiValueCollectors | AutoCollectors} collector - The collector to which the value will be validated
* @returns {function} - A "validator" function that validates the input value
*/
export function returnValidator(
collector: ValidatedTextCollector | ObjectValueCollectors | MultiValueCollectors | AutoCollectors,
) {
const rules = collector.input.validation;
return (value: string | string[] | Record<string, unknown>) => {
return (
rules?.reduce((acc, next) => {
if (next.type === 'required') {
if (
!value ||
(Array.isArray(value) && !value.length) ||
(typeof value === 'object' && !Array.isArray(value) && !Object.keys(value).length)
) {
acc.push(next.message);
}
} else if (next.type === 'regex' && typeof value === 'string') {
try {
const result = new RegExp(next.rule).test(value);
if (!result) {
acc.push(next.message);
}
} catch (err) {
const error = err as Error;
acc.push(error.message);
}
}
return acc;
}, [] as string[]) ?? []
);
};
}
export function returnUnknownCollector(field: Record<string, unknown>, idx: number) {
return {
category: 'UnknownCollector',
error: 'Detected an unknown or unsupported collector type',
type: field['type'],
id: `${field['key'] || field['type']}-${idx}`,
name: field['key'] || field['type'],
output: {
key: `${field['key'] || field['type']}-${idx}`,
label: field['label'] || field['content'],
type: field['type'],
},
} as UnknownCollector;
}