Skip to content

Strict validation is being forgiving #1196

Description

@johnstrickler
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.

Activity

  1. icambron commented on Oct 16, 2013

    @icambron
    Contributor

    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?

  2. ichernev commented on Oct 16, 2013

    @ichernev
    Contributor

    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.

  3. icambron commented on Oct 17, 2013

    @icambron
    Contributor

    I just meant having alternative versions of parseTokenFourDigits and company.

  4. dmahapatro commented on Oct 17, 2013

    @dmahapatro

    FYR: Corresponding jsfiddle

  5. dmahapatro commented on Oct 17, 2013

    @dmahapatro

    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.

  6. johnstrickler commented on Oct 23, 2013

    @johnstrickler
    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions