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

feat: add range error validation for datatypes #1594

Merged
merged 7 commits into from
Jan 31, 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
2 changes: 1 addition & 1 deletion src/data-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export interface DataType {
generateTypeInfo(parameter: ParameterData, options: InternalConnectionOptions): Buffer;
generateParameterLength(parameter: ParameterData, options: InternalConnectionOptions): Buffer;
generateParameterData(parameter: ParameterData, options: InternalConnectionOptions): Generator<Buffer, void>;
validate(value: any, collation: Collation | undefined): any; // TODO: Refactor 'any' and replace with more specific type.
validate(value: any, collation: Collation | undefined, options?: InternalConnectionOptions): any; // TODO: Refactor 'any' and replace with more specific type.

hasTableName?: boolean;

Expand Down
19 changes: 17 additions & 2 deletions src/data-types/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
const NULL_LENGTH = Buffer.from([0x00]);
const DATA_LENGTH = Buffer.from([0x03]);

const MIN_DATE = new globalDate('January 1, 0001');
const MAX_DATE = new globalDate('December 31, 9999');

const Date: DataType = {
id: 0x28,
type: 'DATEN',
Expand Down Expand Up @@ -35,7 +38,7 @@

const value = parameter.value as any; // Temporary solution. Remove 'any' later.

let date;
let date: LocalDate;
if (options.useUTC) {
date = LocalDate.of(value.getUTCFullYear(), value.getUTCMonth() + 1, value.getUTCDate());
} else {
Expand All @@ -49,7 +52,7 @@
},

// TODO: value is technically of type 'unknown'.
validate: function(value): null | Date {
validate: function(value, collation, options): null | Date {
if (value == null) {
return null;
}
Expand All @@ -58,6 +61,18 @@
value = new globalDate(globalDate.parse(value));
}

value = value as Date;

// TODO: check date range: January 1, 0001, through December 31, 9999
// : time range: 00:00:00 through 23:59:59.997
if (options && options.useUTC) {
value = new globalDate(value.toUTCString());

Check warning on line 69 in src/data-types/date.ts

View check run for this annotation

Codecov / codecov/patch

src/data-types/date.ts#L69

Added line #L69 was not covered by tests
}

if (value < MIN_DATE || value > MAX_DATE) {
throw new TypeError('Out of range.');

Check warning on line 73 in src/data-types/date.ts

View check run for this annotation

Codecov / codecov/patch

src/data-types/date.ts#L73

Added line #L73 was not covered by tests
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be RangeError instead? 🤔

}

if (isNaN(value)) {
throw new TypeError('Invalid date.');
}
Expand Down
19 changes: 17 additions & 2 deletions src/data-types/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
const NULL_LENGTH = Buffer.from([0x00]);
const DATA_LENGTH = Buffer.from([0x08]);

const MIN_DATE = new Date('January 1, 1753');
const MAX_DATE = new Date('December 31, 9999');

const DateTime: DataType = {
id: 0x3D,
type: 'DATETIME',
Expand Down Expand Up @@ -34,7 +37,7 @@

const value = parameter.value as any; // Temporary solution. Remove 'any' later.

let date;
let date: LocalDate;
if (options.useUTC) {
date = LocalDate.of(value.getUTCFullYear(), value.getUTCMonth() + 1, value.getUTCDate());
} else {
Expand Down Expand Up @@ -72,7 +75,7 @@
},

// TODO: type 'any' needs to be revisited.
validate: function(value): null | number {
validate: function(value: any, collation, options): null | number {
if (value == null) {
return null;
}
Expand All @@ -81,6 +84,18 @@
value = new Date(Date.parse(value));
}

value = value as Date;

// TODO: check date range: January 1, 1753, through December 31, 9999
// : time range: 00:00:00 through 23:59:59.997
if (options && options.useUTC) {
value = new Date(value.toUTCString());

Check warning on line 92 in src/data-types/datetime.ts

View check run for this annotation

Codecov / codecov/patch

src/data-types/datetime.ts#L92

Added line #L92 was not covered by tests
}

if (value < MIN_DATE || value > MAX_DATE) {
throw new TypeError('Out of range.');

Check warning on line 96 in src/data-types/datetime.ts

View check run for this annotation

Codecov / codecov/patch

src/data-types/datetime.ts#L96

Added line #L96 was not covered by tests
}

if (isNaN(value)) {
throw new TypeError('Invalid date.');
}
Expand Down
19 changes: 17 additions & 2 deletions src/data-types/datetime2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
const EPOCH_DATE = LocalDate.ofYearDay(1, 1);
const NULL_LENGTH = Buffer.from([0x00]);

const MIN_DATE = new Date('January 1, 0001');
const MAX_DATE = new Date('December 31, 9999');

const DateTime2: DataType & { resolveScale: NonNullable<DataType['resolveScale']> } = {
id: 0x2A,
type: 'DATETIME2N',
Expand Down Expand Up @@ -64,7 +67,7 @@
const buffer = new WritableTrackingBuffer(16);
scale = scale!;

let timestamp;
let timestamp: number;
if (options.useUTC) {
timestamp = ((value.getUTCHours() * 60 + value.getUTCMinutes()) * 60 + value.getUTCSeconds()) * 1000 + value.getUTCMilliseconds();
} else {
Expand Down Expand Up @@ -102,7 +105,7 @@
yield buffer.data;
},

validate: function(value): null | number {
validate: function(value: any, collation, options): null | number {
if (value == null) {
return null;
}
Expand All @@ -111,6 +114,18 @@
value = new Date(Date.parse(value));
}

value = value as Date;

// TODO: check date range: January 1, 0001, through December 31, 9999
// : time range: 00:00:00 through 23:59:59.997
if (options && options.useUTC) {
value = new Date(value.toUTCString());

Check warning on line 122 in src/data-types/datetime2.ts

View check run for this annotation

Codecov / codecov/patch

src/data-types/datetime2.ts#L122

Added line #L122 was not covered by tests
}

if (value < MIN_DATE || value > MAX_DATE) {
throw new TypeError('Out of range.');

Check warning on line 126 in src/data-types/datetime2.ts

View check run for this annotation

Codecov / codecov/patch

src/data-types/datetime2.ts#L126

Added line #L126 was not covered by tests
}

if (isNaN(value)) {
throw new TypeError('Invalid date.');
}
Expand Down
19 changes: 17 additions & 2 deletions src/data-types/datetimeoffset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
const EPOCH_DATE = LocalDate.ofYearDay(1, 1);
const NULL_LENGTH = Buffer.from([0x00]);

const MIN_DATE = new Date('January 1, 0001');
const MAX_DATE = new Date('December 31, 9999');

const DateTimeOffset: DataType & { resolveScale: NonNullable<DataType['resolveScale']> } = {
id: 0x2B,
type: 'DATETIMEOFFSETN',
Expand Down Expand Up @@ -62,7 +65,7 @@
const buffer = new WritableTrackingBuffer(16);
scale = scale!;

let timestamp;
let timestamp: number;
timestamp = ((value.getUTCHours() * 60 + value.getUTCMinutes()) * 60 + value.getUTCSeconds()) * 1000 + value.getMilliseconds();
timestamp = timestamp * Math.pow(10, scale - 3);
timestamp += (value.nanosecondDelta != null ? value.nanosecondDelta : 0) * Math.pow(10, scale);
Expand Down Expand Up @@ -92,7 +95,7 @@
buffer.writeInt16LE(offset);
yield buffer.data;
},
validate: function(value): null | number {
validate: function(value: any, collation, options): null | number {
if (value == null) {
return null;
}
Expand All @@ -101,6 +104,18 @@
value = new Date(Date.parse(value));
}

value = value as Date;

// TODO: check date range: January 1, 0001, through December 31, 9999
// : time range: 00:00:00 through 23:59:59.997
if (options && options.useUTC) {
value = new Date(value.toUTCString());

Check warning on line 112 in src/data-types/datetimeoffset.ts

View check run for this annotation

Codecov / codecov/patch

src/data-types/datetimeoffset.ts#L112

Added line #L112 was not covered by tests
}

if (value < MIN_DATE || value > MAX_DATE) {
throw new TypeError('Out of range.');

Check warning on line 116 in src/data-types/datetimeoffset.ts

View check run for this annotation

Codecov / codecov/patch

src/data-types/datetimeoffset.ts#L116

Added line #L116 was not covered by tests
}

if (isNaN(value)) {
throw new TypeError('Invalid date.');
}
Expand Down
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
17 changes: 15 additions & 2 deletions src/data-types/smalldatetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
const EPOCH_DATE = new Date(1900, 0, 1);
const UTC_EPOCH_DATE = new Date(Date.UTC(1900, 0, 1));

const MIN_DATE = new Date(1900, 1, 1);
const MAX_DATE = new Date(2079, 5, 6, 23, 59, 59, 0);

const DATA_LENGTH = Buffer.from([0x04]);
const NULL_LENGTH = Buffer.from([0x00]);

Expand Down Expand Up @@ -35,7 +38,7 @@

const buffer = Buffer.alloc(4);

let days, dstDiff, minutes;
let days: number, dstDiff: number, minutes: number;
if (options.useUTC) {
days = Math.floor((parameter.value.getTime() - UTC_EPOCH_DATE.getTime()) / (1000 * 60 * 60 * 24));
minutes = (parameter.value.getUTCHours() * 60) + parameter.value.getUTCMinutes();
Expand All @@ -51,7 +54,7 @@
yield buffer;
},

validate: function(value): null | Date {
validate: function(value, collation, options): null | Date {
if (value == null) {
return null;
}
Expand All @@ -60,6 +63,16 @@
value = new Date(Date.parse(value));
}

value = value as Date;

if (options && options.useUTC) {
value = new Date(value.toUTCString());

Check warning on line 69 in src/data-types/smalldatetime.ts

View check run for this annotation

Codecov / codecov/patch

src/data-types/smalldatetime.ts#L69

Added line #L69 was not covered by tests
}

if (value < MIN_DATE || value > MAX_DATE) {
throw new TypeError('Out of range.');

Check warning on line 73 in src/data-types/smalldatetime.ts

View check run for this annotation

Codecov / codecov/patch

src/data-types/smalldatetime.ts#L73

Added line #L73 was not covered by tests
}

if (isNaN(value)) {
throw new TypeError('Invalid date.');
}
Expand Down
Loading
Loading