Skip to content

fix: validate date input format before further process - #907

Closed
MichaelSun90 wants to merge 37 commits into
tediousjs:masterfrom
MichaelSun90:dateTypeissue
Closed

MichaelSun90 wants to merge 37 commits into
tediousjs:masterfrom
MichaelSun90:dateTypeissue

Conversation

@MichaelSun90

@MichaelSun90 MichaelSun90 commented Jun 3, 2019 •

Copy link
Copy Markdown
Contributor

Fix the issue mentioned in #889.
If the input Date format is 'YYYY/MM/DD', the current logic directly call math floor on this value will return a wrong date as output.
Added some validation logic before pass through the input value. Also, added some error check for invalid date error.

Added test cases for these handled scenarios

Comment thread test/unit/data-type.js Outdated
['2015/06/18 23:59:59', Buffer.from('03163a0b', 'hex')],
]) {
var buffer = new WritableTrackingBuffer(16);
type.writeParameterData(buffer, { value: value }, { useUTC: false });

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Due to the changes introduced via #845, calling writeParameterData now requires passing a callback function as the last parameter. I think it'd be enough to pass an empty callback function (like () => {}) to fix the test failures here.

@MichaelSun90 MichaelSun90 Jun 10, 2019 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I asked a dumb question. Never mind my previous message. I take a second look on the code, and somehow I missed all the callback function places holders that added to the other test cases.

@IanChokS

This comment has been minimized.

@codecov

codecov Bot commented Aug 19, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #907 into master will increase coverage by 13.49%.
The diff coverage is n/a.

Impacted file tree graph

@@             Coverage Diff             @@
##           master     #907       +/-   ##
===========================================
+ Coverage   65.41%   78.91%   +13.49%     
===========================================
  Files          86       86               
  Lines        4386     4386               
  Branches      793      793               
===========================================
+ Hits         2869     3461      +592     
+ Misses       1162      653      -509     
+ Partials      355      272       -83     
Impacted Files Coverage Δ
src/bulk-load.ts 77.70% <0.00%> (+1.35%) ⬆️
src/connection.ts 58.01% <0.00%> (+1.43%) ⬆️
src/token/stream-parser.ts 74.39% <0.00%> (+1.82%) ⬆️
src/message-io.ts 94.23% <0.00%> (+1.92%) ⬆️
src/request.ts 83.67% <0.00%> (+2.04%) ⬆️
src/outgoing-message-stream.ts 100.00% <0.00%> (+2.43%) ⬆️
src/prelogin-payload.ts 92.78% <0.00%> (+3.09%) ⬆️
src/value-parser.ts 82.29% <0.00%> (+4.16%) ⬆️
src/data-types/binary.ts 76.47% <0.00%> (+5.88%) ⬆️
src/packet.ts 90.52% <0.00%> (+6.31%) ⬆️
... and 39 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f768c6b...f768c6b. Read the comment docs.

@IanChokS

Copy link
Copy Markdown
Member

Lines 389 - 392 in src/bulk-load.ts needs to be updated. 'types' does not have 'validate' method.

@MichaelSun90 MichaelSun90 self-assigned this Aug 30, 2019
Comment thread src/data-types/date.ts Outdated
Comment on lines +27 to +29
if ('Invalid Date' === time.toString()) {
throw new TypeError('Invalid date.');
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can this even happen? The .validate method returns a TypeError if an invalid date was passed. 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is the 'validate' method will parse the input value first, then try to validate the date then?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is there any case that "generateParameterData" can be called independently? is not, then I guess this check is indeed not necessary. 😓

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I removed the unnecessary checks here and in dateTime as well.

Comment thread src/bulk-load.ts Outdated
Comment on lines +95 to +98
const error = c.type.validate(row[i]);
if (error instanceof TypeError) {
throw error;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a breaking change, because validation for bulk loads has never happened before. I agree that we should make this change, but in a separate PR so we can have the version number bumped.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will make a separate PR for this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

A new PR is made # 1065. I have reverted changes from this PR.

Comment thread test/unit/data-type.js Outdated
const buffer = Buffer.concat([...TYPES.Date.generateParameterData(parameter, { useUTC: false })]);
assert.deepEqual(buffer, Buffer.from('03163a0b', 'hex'));
});
it('dateInputWithSlashSeparator', () => {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will fix this, made a mistake in the test name

@arthurschreiber

Copy link
Copy Markdown
Collaborator

I think with the changes in #1065 and validateBulkLoadParameters set to true this is no longer necessary? Please feel free to reopen if you think I'm mistaken. 🙇‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants