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

Strict validation is being forgiving #1196

Closed
johnstrickler opened this Issue Oct 16, 2013 · 6 comments

Comments

Projects
None yet
4 participants
@johnstrickler

johnstrickler commented Oct 16, 2013

moment('2012-05-25', 'YYYY-MM-DD', true).isValid();        // true
moment('201-05-25', 'YYYY-MM-DD', true).isValid();         // true
moment('20-05-25', 'YYYY-MM-DD', true).isValid();          // true
moment('2-05-25', 'YYYY-MM-DD', true).isValid();           // true

I would expect the last 3 lines to be false.

@icambron

This comment has been minimized.

Member

icambron commented Oct 16, 2013

In the current implementation, the strict parser makes sure that all the tokens are used and they consume all the text, but the tokens themselves are still forgiving. For example, "YYYY" matches 1-4 digits.

@ichernev - you mentioned once that we might want separate token definitions for strict. What do you think?

@ichernev

This comment has been minimized.

Contributor

ichernev commented Oct 16, 2013

Well, if we want to move forward from a hacky implementation that sort of works to something better, yes, we do need separate tokens. I'm pretty sure there was a reason why not to use parse -> format -> check for equality, but I can think about one right now.

@icambron

This comment has been minimized.

Member

icambron commented Oct 17, 2013

I just meant having alternative versions of parseTokenFourDigits and company.

@dmahapatro

This comment has been minimized.

dmahapatro commented Oct 17, 2013

FYR: Corresponding jsfiddle

@dmahapatro

This comment has been minimized.

dmahapatro commented Oct 17, 2013

Current workaround:

moment('12/31/2013', 'MM/DD/YYYY', true).isValid() && 
              moment('12/31/2013', 'MM/DD/YYYY').get('y').toString().length == 4;

Comment if there is anything simpler.

@johnstrickler

This comment has been minimized.

johnstrickler commented Oct 23, 2013

Here's an alternative workaround (less robust but simpler):

moment('12/31/2013', 'MM/DD/YYYY', true).isValid()  && /\d{4}$/.test('12/31/2013')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment