thawani-nodejs
Version:
Node.js library for Thawani Payment Gateway
61 lines • 2.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateRequired = validateRequired;
exports.validateAmount = validateAmount;
exports.validateExpiry = validateExpiry;
exports.validateMetadata = validateMetadata;
exports.validateProducts = validateProducts;
const errors_1 = require("./errors");
function validateRequired(params, required) {
for (const field of required) {
if (params[field] === undefined || params[field] === null) {
throw new errors_1.ValidationError(`Missing required field: ${field}`);
}
}
}
function validateAmount(amount) {
if (!Number.isInteger(amount)) {
throw new errors_1.ValidationError('Amount must be an integer');
}
if (amount < 100) {
throw new errors_1.ValidationError('Amount must be at least 100 baisa');
}
}
function validateExpiry(expiryMinutes) {
if (expiryMinutes !== undefined) {
if (!Number.isInteger(expiryMinutes)) {
throw new errors_1.ValidationError('Expiry minutes must be an integer');
}
if (expiryMinutes < 30 || expiryMinutes > 10080) {
throw new errors_1.ValidationError('Expiry minutes must be between 30 and 10080');
}
}
}
function validateMetadata(metadata) {
if (typeof metadata !== 'object' || metadata === null) {
throw new errors_1.ValidationError('Metadata must be an object');
}
}
function validateProducts(products) {
if (!Array.isArray(products)) {
throw new errors_1.ValidationError('Products must be an array');
}
if (products.length === 0) {
throw new errors_1.ValidationError('At least one product is required');
}
if (products.length > 100) {
throw new errors_1.ValidationError('Maximum 100 products allowed');
}
products.forEach((product, index) => {
if (!product.name || typeof product.name !== 'string') {
throw new errors_1.ValidationError(`Invalid product name at index ${index}`);
}
if (!Number.isInteger(product.unit_amount)) {
throw new errors_1.ValidationError(`Invalid unit amount at index ${index}`);
}
if (!Number.isInteger(product.quantity) || product.quantity < 1) {
throw new errors_1.ValidationError(`Invalid quantity at index ${index}`);
}
});
}
//# sourceMappingURL=validation.js.map