Skip to content

Commit 64ecef2

Browse files
committed
chore: fixning codacy issues
1 parent 282e15f commit 64ecef2

File tree

1 file changed

+74
-58
lines changed

1 file changed

+74
-58
lines changed

src/field-set/validation.ts

+74-58
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@ import common from '../common';
33
import { handleTypedError } from '../common/message';
44
import PayTheoryHostedField from '../components/pay-theory-hosted-field';
55
import {
6+
BillingInfo,
67
ErrorResponse,
78
ErrorType,
9+
Level3DataSummary,
810
PayorInfo,
911
TaxIndicatorType,
1012
HealthExpenseType,
1113
} from '../common/pay_theory_types';
12-
import { ModifiedCheckoutDetails, ModifiedTransactProps } from '../common/format';
14+
import {
15+
ModifiedCheckoutDetails,
16+
ModifiedTransactProps,
17+
PayTheoryDataObject,
18+
} from '../common/format';
1319
import { ElementTypes } from '../common/data';
1420

1521
const findField =
@@ -26,8 +32,8 @@ const findAccountType = findField('account-type');
2632
const findAccountName = findField('account-name');
2733

2834
// partner mode is used to indicate migration builds
29-
const checkApiKey = (key: any) => {
30-
if (!validate(key, 'string')) {
35+
const checkApiKey = (key: unknown) => {
36+
if (!validate<string>(key, 'string')) {
3137
throw Error(`Valid API Key not found. Please provide a valid API Key`);
3238
}
3339
const keyParts = key.split('-');
@@ -42,41 +48,50 @@ const checkApiKey = (key: any) => {
4248
}
4349
};
4450

45-
const validate = (value: any, type: string) => {
46-
return typeof value === type && value;
51+
const validate = <T>(value: unknown, type: string): value is T => {
52+
return typeof value === type && Boolean(value);
4753
};
4854

49-
const checkFeeMode = (mode: string) => {
50-
if (!validate(mode, 'string') || ![common.MERCHANT_FEE, common.SERVICE_FEE].includes(mode)) {
55+
const checkFeeMode = (mode: unknown) => {
56+
if (
57+
!validate<string>(mode, 'string') ||
58+
![common.MERCHANT_FEE, common.SERVICE_FEE].includes(mode)
59+
) {
5160
console.error(
5261
`Fee Mode should be either 'merchant_fee' or 'service_fee' which are also available as constants at window.paytheory.MERCHANT_FEE and window.paytheory.SERVICE_FEE`,
5362
);
5463
}
5564
};
5665

57-
const checkMetadata = (metadata: any) => {
66+
const checkMetadata = (metadata: unknown) => {
5867
if (!validate(metadata, 'object')) {
5968
throw Error(`Metadata should be a JSON Object`);
6069
}
6170
};
6271

63-
const checkStyles = (styles: any) => {
72+
const checkStyles = (styles: unknown) => {
6473
if (!validate(styles, 'object')) {
6574
throw Error(
6675
`Styles should be a JSON Object. An example of the object is at https://github.com/pay-theory/payment-components`,
6776
);
6877
}
6978
};
7079

71-
const checkAmount = (amount: any) => {
80+
const checkAmount = (amount: unknown) => {
7281
const error = isValidAmount(amount);
7382

7483
if (error) {
7584
throw Error(error.error);
7685
}
7786
};
7887

79-
const checkInitialParams = (key: any, mode: any, metadata: any, styles: any, amount: any) => {
88+
const checkInitialParams = (
89+
key: unknown,
90+
mode: unknown,
91+
metadata: unknown,
92+
styles: unknown,
93+
amount: unknown,
94+
) => {
8095
checkApiKey(key);
8196
if (mode) checkFeeMode(mode);
8297
checkMetadata(metadata);
@@ -269,25 +284,25 @@ const nullifyEmptyStrings = (params: object) => {
269284
const formatPayorObject = (payorInfo: payorInfo): PayorInfo => {
270285
// Make a deep copy of the payorInfo object
271286
let payorCopy = JSON.parse(JSON.stringify(payorInfo));
272-
// Nullify any empty strings
287+
// Nullify unknown empty strings
273288
payorCopy = nullifyEmptyStrings(payorCopy);
274-
// Strip out any non-numeric characters from the phone number
289+
// Strip out unknown non-numeric characters from the phone number
275290
if (payorCopy.phone) {
276291
payorCopy.phone = payorCopy.phone.replace(/\D/g, '');
277292
}
278293
return payorCopy;
279294
};
280295

281296
const isvalidTransactParams = (
282-
amount: any,
283-
payorInfo: any,
284-
metadata: any,
297+
amount: unknown,
298+
payorInfo: unknown,
299+
metadata: unknown,
285300
): ErrorResponse | null => {
286301
//Make sure that we have the base required settings
287302
if (
288-
!validate(amount, 'number') ||
289-
!validate(metadata, 'object') ||
290-
!validate(payorInfo, 'object')
303+
!validate<number>(amount, 'number') ||
304+
!validate<object>(metadata, 'object') ||
305+
!validate<object>(payorInfo, 'object')
291306
) {
292307
const missing = `${!validate(amount, 'number') ? 'amount ' : ''}${!validate(metadata, 'object') ? 'metadata ' : ''}${!validate(payorInfo, 'object') ? 'payorInfo ' : ''}`;
293308
return handleTypedError(
@@ -301,7 +316,7 @@ const isvalidTransactParams = (
301316
return null;
302317
};
303318

304-
const isValidTokenizeParams = (payorInfo: any, metadata: any): ErrorResponse | null => {
319+
const isValidTokenizeParams = (payorInfo: unknown, metadata: unknown): ErrorResponse | null => {
305320
//Make sure that we have the base required settings
306321
if (!validate(metadata, 'object') || !validate(payorInfo, 'object')) {
307322
const missing = `${!validate(metadata, 'object') ? 'metadata ' : ''}${!validate(payorInfo, 'object') ? 'payorInfo ' : ''}`;
@@ -313,8 +328,8 @@ const isValidTokenizeParams = (payorInfo: any, metadata: any): ErrorResponse | n
313328
return null;
314329
};
315330

316-
const isValidAmount = (amount: any): ErrorResponse | null => {
317-
if (!validate(amount, 'number')) {
331+
const isValidAmount = (amount: unknown): ErrorResponse | null => {
332+
if (!validate<number>(amount, 'number')) {
318333
return handleTypedError(ErrorType.INVALID_PARAM, 'amount must be a positive integer');
319334
}
320335
if (amount % 1 !== 0) {
@@ -323,14 +338,14 @@ const isValidAmount = (amount: any): ErrorResponse | null => {
323338
return null;
324339
};
325340

326-
const isValidDeviceId = (deviceId: any): ErrorResponse | null => {
327-
if (!validate(deviceId, 'string')) {
328-
return handleTypedError(ErrorType.INVALID_PARAM, 'deviceId is required and must be a string');
329-
}
330-
return null;
331-
};
341+
// const isValidDeviceId = (deviceId: unknown): ErrorResponse | null => {
342+
// if (!validate(deviceId, 'string')) {
343+
// return handleTypedError(ErrorType.INVALID_PARAM, 'deviceId is required and must be a string');
344+
// }
345+
// return null;
346+
// };
332347

333-
const isValidPayorDetails = (payorInfo: any, payorId: any): ErrorResponse | null => {
348+
const isValidPayorDetails = (payorInfo: unknown, payorId: unknown): ErrorResponse | null => {
334349
const keys = Object.keys(payorInfo);
335350
// Verify both id and info aren't passed in
336351
if (payorId && keys.length > 0) {
@@ -345,15 +360,15 @@ const isValidPayorDetails = (payorInfo: any, payorId: any): ErrorResponse | null
345360
return null;
346361
};
347362

348-
const isValidInvoiceAndRecurringId = (payTheoryInfo: any): ErrorResponse | null => {
349-
const { invoiceId, recurringId } = payTheoryInfo;
350-
if (invoiceId && !validate(invoiceId, 'string')) {
363+
const isValidInvoiceAndRecurringId = (payTheoryInfo: PayTheoryDataObject): ErrorResponse | null => {
364+
const { invoice_id, recurring_id } = payTheoryInfo;
365+
if (invoice_id && !validate(invoice_id, 'string')) {
351366
return handleTypedError(ErrorType.INVALID_PARAM, 'invoiceId must be a string');
352367
}
353-
if (recurringId && !validate(recurringId, 'string')) {
368+
if (recurring_id && !validate(recurring_id, 'string')) {
354369
return handleTypedError(ErrorType.INVALID_PARAM, 'recurringId must be a string');
355370
}
356-
if (invoiceId && recurringId) {
371+
if (invoice_id && recurring_id) {
357372
return handleTypedError(
358373
ErrorType.INVALID_PARAM,
359374
'invoiceId and recurringId cannot both be present',
@@ -362,7 +377,7 @@ const isValidInvoiceAndRecurringId = (payTheoryInfo: any): ErrorResponse | null
362377
return null;
363378
};
364379

365-
const isValidFeeMode = (feeMode: any): ErrorResponse | null => {
380+
const isValidFeeMode = (feeMode: string): ErrorResponse | null => {
366381
if (![common.MERCHANT_FEE, common.SERVICE_FEE].includes(feeMode)) {
367382
return handleTypedError(
368383
ErrorType.INVALID_PARAM,
@@ -372,34 +387,36 @@ const isValidFeeMode = (feeMode: any): ErrorResponse | null => {
372387
return null;
373388
};
374389

375-
const isValidFeeAmount = (fee: any): ErrorResponse | null => {
390+
const isValidFeeAmount = (fee: unknown): ErrorResponse | null => {
376391
if ((fee || typeof fee === 'number') && !validate(fee, 'number')) {
377392
return handleTypedError(ErrorType.INVALID_PARAM, 'fee must be a positive integer');
378393
}
379394
return null;
380395
};
381396

382-
const isValidBillingInfo = (billingInfo: any): ErrorResponse | null => {
383-
if (billingInfo && !validate(billingInfo, 'object')) {
384-
return handleTypedError(ErrorType.INVALID_PARAM, 'billingInfo must be an object');
385-
}
386-
if (billingInfo && billingInfo.address && !validate(billingInfo.address, 'object')) {
387-
return handleTypedError(ErrorType.INVALID_PARAM, 'billingInfo.address must be an object');
388-
}
389-
if (billingInfo && billingInfo.address && !validate(billingInfo.address.postal_code, 'string')) {
390-
return handleTypedError(
391-
ErrorType.INVALID_PARAM,
392-
'billingInfo.address.postal_code is required when passing in billingInfo',
393-
);
397+
const isValidBillingInfo = (billingInfo: unknown): ErrorResponse | null => {
398+
if (billingInfo) {
399+
if (!validate<BillingInfo>(billingInfo, 'object')) {
400+
return handleTypedError(ErrorType.INVALID_PARAM, 'billingInfo must be an object');
401+
}
402+
if (billingInfo.address && !validate(billingInfo.address, 'object')) {
403+
return handleTypedError(ErrorType.INVALID_PARAM, 'billingInfo.address must be an object');
404+
}
405+
if (billingInfo.address && !validate(billingInfo.address.postal_code, 'string')) {
406+
return handleTypedError(
407+
ErrorType.INVALID_PARAM,
408+
'billingInfo.address.postal_code is required when passing in billingInfo',
409+
);
410+
}
394411
}
395412
return null;
396413
};
397414

398-
const isValidL3DataSummary = (l3DataSummary: any): ErrorResponse | null => {
415+
const isValidL3DataSummary = (l3DataSummary: unknown): ErrorResponse | null => {
399416
// If null or undefined then return null
400417
if (l3DataSummary === null || l3DataSummary === undefined) return null;
401418

402-
if (!validate(l3DataSummary, 'object')) {
419+
if (!validate<Level3DataSummary>(l3DataSummary, 'object')) {
403420
return handleTypedError(ErrorType.INVALID_PARAM, 'l3DataSummary must be an object');
404421
}
405422

@@ -459,11 +476,11 @@ const isValidL3DataSummary = (l3DataSummary: any): ErrorResponse | null => {
459476
return null;
460477
};
461478

462-
const isValidHealthExpenseType = (healthExpenseType: any): ErrorResponse | null => {
479+
const isValidHealthExpenseType = (healthExpenseType: unknown): ErrorResponse | null => {
463480
if (healthExpenseType === null || healthExpenseType === undefined) return null;
464-
if (!validate(healthExpenseType, 'string')) {
481+
if (!validate<string>(healthExpenseType, 'string')) {
465482
return handleTypedError(ErrorType.INVALID_PARAM, 'healthExpenseType must be a string');
466-
}
483+
} //@ts-expect-error
467484
if (!Object.values(HealthExpenseType).includes(healthExpenseType)) {
468485
return handleTypedError(
469486
ErrorType.INVALID_PARAM,
@@ -475,9 +492,9 @@ const isValidHealthExpenseType = (healthExpenseType: any): ErrorResponse | null
475492
};
476493

477494
const validateHostedCheckoutParams = (
478-
callToAction: any,
479-
acceptedPaymentMethods: any,
480-
paymentName: any,
495+
callToAction: string,
496+
acceptedPaymentMethods: string,
497+
paymentName: unknown,
481498
): ErrorResponse | null => {
482499
if (callToAction && !common.CTA_TYPES.includes(callToAction)) {
483500
return handleTypedError(
@@ -533,7 +550,7 @@ const validTransactionParams = (
533550
return isValidFeeAmount(payTheoryData?.fee);
534551
};
535552

536-
const validQRSize = (size: any): ErrorResponse | null => {
553+
const validQRSize = (size: unknown): ErrorResponse | null => {
537554
if (!validate(size, 'number')) {
538555
return handleTypedError(ErrorType.INVALID_PARAM, 'size must be a number');
539556
}
@@ -550,7 +567,6 @@ export {
550567
formatPayorObject,
551568
validate,
552569
isValidAmount,
553-
isValidDeviceId,
554570
isvalidTransactParams,
555571
isValidTokenizeParams,
556572
isValidPayorInfo,

0 commit comments

Comments
 (0)