-
Notifications
You must be signed in to change notification settings - Fork 41
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
base: main
Are you sure you want to change the base?
Conversation
@@ -24,6 +24,31 @@ interface StageConfig { | |||
variableWalletLimitPath?: string; | |||
} | |||
|
|||
function isValidDate(dateString: string): boolean { | |||
const date = new Date(dateString); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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") === true
because 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.
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