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

adding validation to setStages #137

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

FwazB
Copy link

@FwazB FwazB commented Jun 28, 2024

Added Validation helper functions to define Date, then implemented logic to make sure the startDate is after the endDate. Finally implemented the logic into the setStages function

@@ -24,6 +24,31 @@ interface StageConfig {
variableWalletLimitPath?: string;
}

function isValidDate(dateString: string): boolean {
const date = new Date(dateString);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this able to catch 2024-06-31 issue?

Copy link
Author

Choose a reason for hiding this comment

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

https://stackoverflow.com/questions/1214234/javascript-date-parsing-bug-fails-for-dates-in-june

Should be good based on this article, defining and using that will fix

Copy link
Contributor

Choose a reason for hiding this comment

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

image Seems `new Date()` will not fail

Choose a reason for hiding this comment

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

@FwazB I have suffered immensely from JS dates. It's a "whack a mole" problem. There's always another gotcha.

I am seeing seconds (from block.timestamp) used and date strings like @channing-magiceden is calling out. Something like this could be helpful, modify as necessary:

function isValidDate(date: string) {
  if (/^\d+$/.test(date)) {
    // Date expects milliseconds, * 1000 here if using seconds
    const milliSeconds = parseInt(date, 10);

    return isValidDateInMilliSeconds(milliSeconds);
  } else {
    return isValidDateString(date);
  }
}

function isValidDateInMilliSeconds(milliseconds: number) {
  return !isNaN(new Date(milliseconds).getDate());
}

function isValidDateString(dateString: string) {
  const [year, month, day] = dateString.split(/[-/]/).map(Number);
  const date = new Date(year, month - 1, day);

  if (
    !(
      date.getFullYear() === year &&
      date.getMonth() + 1 === month &&
      date.getDate() === day
    )
  ) {
    return false;
  } else {
    return !isNaN(date.getTime());
  }
}

But this has some things to be aware of as well:

isValidDate("20240631") === truebecause the regex will treat this as milliseconds.

But in general, with js dates you can split/compare the input against the date values to ensure they match.

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