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

Added date validation #93

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,25 @@ export class ConversationForm<C extends Context> {
}
return url;
}

/**
* Waits until the user sends a a valid Date, and returns this Date. If the user sends
* something that cannot be parsed to a Date by the global `Date` constructor,
* these updates will be skipped. You may specify the `otherwise` handler
* that is called in such cases. Among other things, this allows you to tell
* the user that they did not send a valid Date.
*
* @param otherwise Handler that will be run for skipped updates
* @returns The received date
*/
async date(otherwise?: (ctx: C) => unknown | Promise<unknown>) {
const ctx = await this.conversation.wait();
const text = ctx.msg?.text ?? ctx.msg?.caption ?? "";
let date = new Date(text);
if(date === "Invalid Date") {
Copy link
Member

Choose a reason for hiding this comment

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

This comparison looks sketchy, though, are you sure that dates work like that?

Copy link
Author

Choose a reason for hiding this comment

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

Fixed converting date to a string

await otherwise?.(ctx);
return await this.conversation.skip();
}
return date;
}
}
Loading