UNPKG

@yuju/tosspayments-sdk

Version:

Toss Payments SDK for Node.js

96 lines (95 loc) 4.26 kB
import { ValidationError } from '../utils/validationError.js'; // Helper function to validate the constraints export const validateCancelRequestBody = (body) => { if (body.cancelReason.length > 200) throw new ValidationError('cancelReason is too long'); if (body.refundReceiveAccount) { if (body.refundReceiveAccount.accountNumber.length > 20 || /\D/.test(body.refundReceiveAccount.accountNumber)) throw new ValidationError('accountNumber is invalid'); if (body.refundReceiveAccount.holderName.length > 60) throw new ValidationError('holderName is too long'); } if (body.currency && body.currency !== 'USD') throw new ValidationError('currency is invalid'); }; export function validateIssueVirtualAccountRequest(body) { const errors = []; // Validate amount if (body.amount <= 0) { errors.push("Invalid amount: It should be a positive integer."); } // Validate orderId if (!body.orderId || body.orderId.length < 6 || body.orderId.length > 64 || !/^[a-zA-Z0-9-_]+$/.test(body.orderId)) { errors.push("Invalid orderId: It should be a string of 6-64 characters and can include letters, numbers, -, and _."); } // Validate orderName if (!body.orderName || body.orderName.length < 1 || body.orderName.length > 100) { errors.push("Invalid orderName: It should be a string of 1-100 characters."); } // Validate customerName if (!body.customerName || body.customerName.length > 100) { errors.push("Invalid customerName: It should be a string with up to 100 characters."); } // Validate bank if (!body.bank) { errors.push("Invalid bank: Bank name is required."); } // Validate accountKey if (body.accountKey && body.accountKey.length > 13) { errors.push("Invalid accountKey: It should be a string with up to 13 characters."); } // Validate validHours and dueDate if (body.validHours && body.dueDate) { errors.push("Only one of validHours or dueDate should be provided."); } else if (body.validHours && (body.validHours <= 0 || body.validHours > 720)) { errors.push("Invalid validHours: It should be between 1 and 720."); } else if (body.dueDate && !/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/.test(body.dueDate)) { errors.push("Invalid dueDate: It should be in yyyy-MM-dd'T'HH:mm:ss format."); } // Validate customerEmail if (body.customerEmail && body.customerEmail.length > 100) { errors.push("Invalid customerEmail: It should be a string with up to 100 characters."); } // [Continue with the remaining validations...] if (errors.length > 0) { throw new ValidationError(errors.join(' ')); } } export function validateIssueBillingKeyWithCustomerKey(data) { const customerKeyPattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[-_=.@]).{2,300}$/; const yearPattern = /^\d{4}$/; const monthPattern = /^(0[1-9]|1[0-2])$/; if (!customerKeyPattern.test(data.customerKey)) { throw new ValidationError("Invalid customerKey."); } if (data.cardNumber.length > 20) { throw new ValidationError("Invalid cardNumber length."); } if (!yearPattern.test(data.cardExpirationYear)) { throw new ValidationError("Invalid cardExpirationYear."); } if (!monthPattern.test(data.cardExpirationMonth)) { throw new ValidationError("Invalid cardExpirationMonth."); } const idLength = data.customerIdentityNumber.length; if (idLength !== 6 && idLength !== 10) { throw new ValidationError("Invalid customerIdentityNumber length."); } if (data.cardPassword && data.cardPassword.length !== 2) { throw new ValidationError("Invalid cardPassword length."); } if (data.customerName && data.customerName.length > 100) { throw new ValidationError("Invalid customerName length."); } if (data.customerEmail && data.customerEmail.length > 100) { throw new ValidationError("Invalid customerEmail length."); } if (data.vbv) { if (!data.vbv.cavv || !data.vbv.xid || !data.vbv.eci) { throw new ValidationError("Invalid vbv data."); } } }