Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@ describe('validateStringAmount', () => {

it('should reject invalid amounts', () => {
expect(() => validateStringAmount('0', 6)).toThrow('Invalid amount: must be greater than 0');
expect(() => validateStringAmount('00.000000', 6)).toThrow(
'Invalid amount: must be greater than 0'
);
expect(() => validateStringAmount('-10', 6)).toThrow('Invalid amount: must be greater than 0');
expect(() => validateStringAmount('abc', 6)).toThrow('Invalid amount: must be a valid number');
expect(() => validateStringAmount('10.1234567', 6)).toThrow(
'Invalid amount: pay only supports up to 6 decimal places'
);
});

it.each(['1abc', '1foo', '1.2.3', '1e6', '1.', '.1', '.', '', ' 1', '1 ', 'Infinity'])(
'should reject malformed amount %s',
(amount) => {
expect(() => validateStringAmount(amount, 6)).toThrow(
'Invalid amount: must be a valid number'
);
}
);

it('should reject non-string amounts', () => {
expect(() => validateStringAmount(10 as any, 6)).toThrow('Invalid amount: must be a string');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ export function validateStringAmount(amount: string, maxDecimals: number): void
throw new Error('Invalid amount: must be a string');
}

const numAmount = parseFloat(amount);

if (isNaN(numAmount)) {
if (!/^-?\d+(?:\.\d+)?$/.test(amount)) {
throw new Error('Invalid amount: must be a valid number');
}

if (numAmount <= 0) {
if (amount.startsWith('-') || !/[1-9]/.test(amount)) {
throw new Error('Invalid amount: must be greater than 0');
}

Expand Down