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

Date assert should be able to handle unix timestamp #93

Open
madeiras opened this issue Apr 6, 2016 · 6 comments
Open

Date assert should be able to handle unix timestamp #93

madeiras opened this issue Apr 6, 2016 · 6 comments

Comments

@madeiras
Copy link

madeiras commented Apr 6, 2016

No description provided.

@ruimarinho
Copy link
Member

@JoaoHenriquePereira can you submit a PR adding moment parse to each Date assert instead of what's being done currently?

@madeiras
Copy link
Author

Actually I took some time today to think about this. WDYT something like this:

this.validate = value => {
    if (typeof value === 'string') {
      if (isNaN(Date.parse(value)) === true) {
        throw new Violation(this, value);
      }

      return true;
    }

    if (typeof value === 'number') {
      if (new Date(value).getTime() < 0) {
        throw new Violation(this, value);
      }

      return true;
    }

    if (Object.prototype.toString.call(value) !== '[object Date]') {
      throw new Violation(this, value, { value: 'must_be_a_date_or_a_string_or_a_timestamp' });
    }

    return true;
  };

It keeps compatibility, adds support to evaluate a timestamp and evaluates a Date object in similar fashion to moment isDate (as before). The caveat I see is the number being anything and being parsed as a timestamp.... Might be easier to just trust moment though I haven't explored thoroughly yet.

@madeiras
Copy link
Author

Update: Ok noticed you changed the assert already :) I'll update later what needs to be done.

@ruimarinho
Copy link
Member

Something like that would work, yes. That would work for server as minor, otherwise adding moment has the caveat of making it a major (requires additional peer dependency).

@ruimarinho
Copy link
Member

Note this from MDN:

Date.prototype.getTime()

Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC (negative for prior times).

@madeiras
Copy link
Author

I'll have a look on how I could add support for negative dates and submit a PR.

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

No branches or pull requests

2 participants