Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix bigint validation #1620

Merged
merged 1 commit into from
Apr 18, 2024
Merged
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
16 changes: 7 additions & 9 deletions src/data-types/bigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import WritableTrackingBuffer from '../tracking-buffer/writable-tracking-buffer'

const DATA_LENGTH = Buffer.from([0x08]);
const NULL_LENGTH = Buffer.from([0x00]);
const MAX_SAFE_BIGINT = 9223372036854775807n;
const MIN_SAFE_BIGINT = -9223372036854775808n;

const BigInt: DataType = {
id: 0x7F,
Expand Down Expand Up @@ -36,21 +38,17 @@ const BigInt: DataType = {
yield buffer.data;
},

validate: function(value): null | number {
validate: function(value): null | bigint {
if (value == null) {
return null;
}

if (typeof value !== 'number') {
value = Number(value);
if (typeof value !== 'bigint') {
value = globalThis.BigInt(value);
}

if (isNaN(value)) {
throw new TypeError('Invalid number.');
}

if (value < Number.MIN_SAFE_INTEGER || value > Number.MAX_SAFE_INTEGER) {
throw new TypeError(`Value must be between ${Number.MIN_SAFE_INTEGER} and ${Number.MAX_SAFE_INTEGER}, inclusive. For smaller or bigger numbers, use VarChar type.`);
if (value < MIN_SAFE_BIGINT || value > MAX_SAFE_BIGINT) {
throw new TypeError(`Value must be between ${MIN_SAFE_BIGINT} and ${MAX_SAFE_BIGINT}, inclusive.`);
}

return value;
Expand Down
22 changes: 13 additions & 9 deletions test/unit/validations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,25 @@ describe('Validations', function() {
assert.strictEqual(value, null);

value = TYPE.BigInt.validate(2147483647);
assert.strictEqual(value, 2147483647);
assert.strictEqual(value, 2147483647n);

value = TYPE.BigInt.validate(-9223372036854775808n);
assert.strictEqual(value, -9223372036854775808n);

value = TYPE.BigInt.validate(-9007199254740991);
assert.strictEqual(value, -9007199254740991);
value = TYPE.BigInt.validate(9223372036854775807n);
assert.strictEqual(value, 9223372036854775807n);

value = TYPE.BigInt.validate(9007199254740991);
assert.strictEqual(value, 9007199254740991);
assert.throws(() => {
TYPE.BigInt.validate(-9223372036854775809n);
}, TypeError, 'Value must be between -9223372036854775808 and 9223372036854775807, inclusive.');

assert.throws(() => {
TYPE.BigInt.validate(-9007199254740992);
}, TypeError, 'Value must be between -9007199254740991 and 9007199254740991, inclusive. For smaller or bigger numbers, use VarChar type.');
TYPE.BigInt.validate(9223372036854775808n);
}, TypeError, 'Value must be between -9223372036854775808 and 9223372036854775807, inclusive.');

assert.throws(() => {
TYPE.BigInt.validate(9007199254740992);
}, TypeError, 'Value must be between -9007199254740991 and 9007199254740991, inclusive. For smaller or bigger numbers, use VarChar type.');
TYPE.BigInt.validate(0.5);
}, RangeError, 'The number 0.5 cannot be converted to a BigInt because it is not an integer');
});

it('SmallDateTime', () => {
Expand Down
Loading