Skip to content

Commit

Permalink
add money validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mShan0 committed Dec 5, 2023
1 parent 6effc24 commit 3074459
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/data-types/money.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ const Money: DataType = {
if (isNaN(value)) {
throw new TypeError('Invalid number.');
}
// money: -922337203685477.5808 to 922337203685477.5807
// in javascript -922337203685477.5808 === -922337203685477.6
// 922337203685477.5807 === 922337203685477.6
// javascript number doesn't have enough precision.
if (value < -922337203685477.6 || value > 922337203685477.6) {
throw new TypeError('Value must be between -922337203685477.5808 and 922337203685477.5807, inclusive.');
}

return value;
}
};
Expand Down
19 changes: 19 additions & 0 deletions test/unit/data-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,25 @@ describe('Money', function() {
assert.deepEqual(result, expected);
});
});

describe.only('.validate', function() {
it('throws Invalid number error for NaN input', function() {
assert.throws(() => {
TYPES.TinyInt.validate('string');
}, TypeError, 'Invalid number.');
});

it('throws Out of Range error for numbers out of range', function() {
assert.throws(() => {

TYPES.Money.validate(-922337203685477.5808 - 0.1);
}, TypeError, 'Value must be between -922337203685477.5808 and 922337203685477.5807, inclusive.');

assert.throws(() => {
TYPES.Money.validate(922337203685477.5807 + 0.1);
}, TypeError, 'Value must be between -922337203685477.5808 and 922337203685477.5807, inclusive.');
});
});
});

describe('NChar', function() {
Expand Down

0 comments on commit 3074459

Please sign in to comment.