Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

fix(dateparser): add type and validity check #3759

Conversation

wesleycho
Copy link
Contributor

  • Add type and validity check to ensure proper date object is created
  • Make code style uniform in scripts

Plunker with fix in action here

This addresses #3713

@wesleycho
Copy link
Contributor Author

I believe this also closes #3701, and fixes #3609, #3736, #3071.

@rvanbaalen
Copy link
Contributor

Leaving a comment to keep track on this one. Will review this saturday.

@chrisirhc
Copy link
Contributor

@wesleycho , there's a lot of whitespace changes in this PR. Any chance of editing that away? Just want to avoid adding noise to git blame.

@chrisirhc
Copy link
Contributor

Code change looks good to me.
I was only wondering if this should log a warning when the base date argument is provided but invalid (isNaN check returns true).

@wesleycho
Copy link
Contributor Author

Sure, I can do that sometime later today or tomorrow.

I can add a $log.debug or $log.warn statement about a string where isNaN returns true and falling back to date constructor.

@wesleycho
Copy link
Contributor Author

I reverted all code style changes

@wesleycho wesleycho force-pushed the fix/dateparser-graceful-conditionals branch 2 times, most recently from 09919d0 to b59efc1 Compare June 8, 2015 01:55
@@ -136,6 +136,9 @@ angular.module('ui.bootstrap.dateparser', [])
milliseconds: baseDate.getMilliseconds()
};
} else {
if (baseDate) {
$log.warn('dateparser:', 'baseDate is a string with no date representation');
Copy link
Contributor

Choose a reason for hiding this comment

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

How about baseDate is not a valid date ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Much better than what I had - changing shortly

- Add type and validity check to ensure proper date object is created
- Make code style uniform in scripts

chore(dateparser): revert code style changes

chore(dateparser): add `$log.warn`
@wesleycho wesleycho force-pushed the fix/dateparser-graceful-conditionals branch from b59efc1 to 3dbfb80 Compare June 8, 2015 12:57
@chrisirhc
Copy link
Contributor

Hm, I was just testing this, I think this introduces another bug, now there's time added to the string, possibly due to a toISOString conversion that shouldn't be done:

See http://plnkr.co/edit/XCvfhGTYUUraB6mTSVSK?p=preview (there are steps to reproduce on the Plunker)

I'm thinking the change in #3294 is needed to actually fix this at the root.

@wesleycho
Copy link
Contributor Author

I tried some experimenting, and it looks like stripping out the || new Date(...) portions from the $viewChangeListener and $parser does not fix this issue at all. In fact, this looks to be a preexisting issue in the 0.13.0 release itself, due to the fact that the view value gets converted to a date as the model value.

@wesleycho
Copy link
Contributor Author

#3643 may also be related.

@cbosco
Copy link

cbosco commented Jun 24, 2015

+1

@IRus
Copy link

IRus commented Jul 16, 2015

Hi, don't understand this check:

if (angular.isDate(baseDate) && !isNaN(baseDate.getTime())) {

if baseDate is date getTime always will return an number, right?

@wesleycho
Copy link
Contributor Author

That is not true - try doing new Date('foo').getTime()

@IRus
Copy link

IRus commented Jul 16, 2015

Yep, you right, Thank you for example.

@wesleycho
Copy link
Contributor Author

@chrisirhc what are your thoughts on this? I do not think that the model aspect is fixable, since the model value will always become a date object or null currently (even if the new Date(...) fallback is stripped).

This can potentially be alleviated if we are able to switch to allowing users to bring in a custom parser & formatter for converting between a user desired model and the view value, which would also open up moment.js support for users, but that is a large & separate task.

I think this PR should make it in in its current incarnation though - it fixes a badly breaking bug in the datepicker.

@IRus
Copy link

IRus commented Jul 20, 2015

@wesleycho please remove || new Date(...) "fallback" that actually introducing new bugs.

@wesleycho
Copy link
Contributor Author

That is a breaking change that has explicit tests written against it - it is outside the scope of this PR.

@RobJacobs RobJacobs mentioned this pull request Jul 22, 2015
@wesleycho wesleycho modified the milestones: 0.13.1 (npm), 0.13.2 (Performance) Jul 23, 2015
@anilnatha
Copy link

Is this in any way related to Issue #3617?

Reason I ask is because one of the behaviors of the bug illustrated in 3617 is that when you clear a datepicker field, it sets a form's $invalid property to false, even though the field is blank and the field is marked as not required. I created a plunker illustrating the issue and is posted in that thread.

@llafuente
Copy link

Partially related.
I apply this patch, and the exception is gone, but form is still $invalid.

@wesleycho
Copy link
Contributor Author

@llafuente I am not seeing the form being invalid incorrectly - here is an updated Plunker.

@llafuente
Copy link

This patch alone do not solve: #3617

I apply this patch to 0.13, and didn't work, form was $invalid.
I can confirm master + this patch works.

@wesleycho
Copy link
Contributor Author

master is the only version that matters. There are a lot of changes since 0.13.0.

@wesleycho
Copy link
Contributor Author

Merging this in shortly, as this check in itself is completely safe & necessary.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants