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: date range check #1600

Merged
merged 8 commits into from
Feb 9, 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
12 changes: 5 additions & 7 deletions src/data-types/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
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 @@ -63,13 +60,14 @@

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
let year;
if (options && options.useUTC) {
value = new globalDate(value.toUTCString());
year = value.getUTCFullYear();

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

View check run for this annotation

Codecov / codecov/patch

src/data-types/date.ts#L65

Added line #L65 was not covered by tests
} else {
year = value.getFullYear();
}

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

Expand Down
12 changes: 5 additions & 7 deletions src/data-types/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
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 @@ -86,13 +83,14 @@

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
let year;
if (options && options.useUTC) {
value = new Date(value.toUTCString());
year = value.getUTCFullYear();

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

View check run for this annotation

Codecov / codecov/patch

src/data-types/datetime.ts#L88

Added line #L88 was not covered by tests
} else {
year = value.getFullYear();
}

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

Expand Down
12 changes: 5 additions & 7 deletions src/data-types/datetime2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
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 @@ -116,13 +113,14 @@

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
let year;
if (options && options.useUTC) {
value = new Date(value.toUTCString());
year = value.getUTCFullYear();

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

View check run for this annotation

Codecov / codecov/patch

src/data-types/datetime2.ts#L118

Added line #L118 was not covered by tests
} else {
year = value.getFullYear();
}

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

Expand Down
12 changes: 5 additions & 7 deletions src/data-types/datetimeoffset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
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 @@ -106,13 +103,14 @@

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
let year;
if (options && options.useUTC) {
value = new Date(value.toUTCString());
year = value.getUTCFullYear();

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

View check run for this annotation

Codecov / codecov/patch

src/data-types/datetimeoffset.ts#L108

Added line #L108 was not covered by tests
} else {
year = value.getFullYear();
}

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

Expand Down
21 changes: 16 additions & 5 deletions src/data-types/smalldatetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
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 @@ -65,14 +62,28 @@

value = value as Date;

let year, month, date;
if (options && options.useUTC) {
value = new Date(value.toUTCString());
year = value.getUTCFullYear();
month = value.getUTCMonth();
date = value.getUTCDate();

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#L67-L69

Added lines #L67 - L69 were not covered by tests
} else {
year = value.getFullYear();
month = value.getMonth();
date = value.getDate();
}

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

if (year === 2079) {
// Month is 0-indexed, i.e. Jan = 0, Dec = 11
if (month > 4 || (month === 4 && date > 6)) {
throw new TypeError('Out of range.');
}
}

if (isNaN(value)) {
throw new TypeError('Invalid date.');
}
Expand Down
24 changes: 19 additions & 5 deletions test/unit/data-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ describe('Date', function() {
describe('.validate', function() {
it('returns a TypeError for dates that are out of range', function() {
assert.throws(() => {
TYPES.Date.validate(new Date('Dec 31 2000'));
const testDate = new Date();
testDate.setFullYear(0);
TYPES.Date.validate(testDate);
}, TypeError, 'Out of range.');

assert.throws(() => {
Expand Down Expand Up @@ -320,7 +322,9 @@ describe('DateTime2', function() {
describe('.validate', function() {
it('returns a TypeError for dates that are out of range', function() {
assert.throws(() => {
TYPES.DateTime2.validate(new Date('Dec 31, 2000'));
const testDate = new Date();
testDate.setFullYear(0);
TYPES.DateTime2.validate(testDate);
}, TypeError, 'Out of range.');

assert.throws(() => {
Expand Down Expand Up @@ -385,7 +389,9 @@ describe('DateTimeOffset', function() {
describe('.validate', function() {
it('returns a TypeError for dates that are out of range', function() {
assert.throws(() => {
TYPES.DateTimeOffset.validate(new Date('Dec 31, 2000'));
const testDate = new Date();
testDate.setFullYear(0);
TYPES.DateTimeOffset.validate(testDate);
}, TypeError, 'Out of range.');

assert.throws(() => {
Expand Down Expand Up @@ -742,7 +748,7 @@ describe('Money', function() {
});
});

describe.only('.validate', function() {
describe('.validate', function() {
it('throws Invalid number error for NaN input', function() {
assert.throws(() => {
TYPES.TinyInt.validate('string');
Expand Down Expand Up @@ -1067,7 +1073,15 @@ describe('SmallDateTime', function() {
}, TypeError, 'Out of range.');

assert.throws(() => {
TYPES.SmallDateTime.validate(new Date('June 7, 2079'));
TYPES.SmallDateTime.validate(new Date('May 7, 2079'));
}, TypeError, 'Out of range.');

assert.throws(() => {
TYPES.SmallDateTime.validate(new Date('Jan 1, 2080'));
}, TypeError, 'Out of range.');

assert.throws(() => {
TYPES.SmallDateTime.validate(new Date('June 1, 2079'));
}, TypeError, 'Out of range.');
});
});
Expand Down
Loading