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

feat: add isCronTimeValid function to validate cron expressions #959

Merged
merged 4 commits into from
Feb 24, 2025

Conversation

WillianAgostini
Copy link
Contributor

Description

This PR adds a function isCronTimeValid to validate CRON expressions without using try/catch blocks.

Related Issue

Issue #958

Motivation and Context

Makes it easier to validate CRON expressions

How Has This Been Tested?

Added two new test cases

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • If my change introduces a breaking change, I have added a ! after the type/scope in the title (see the Conventional Commits standard).

@WillianAgostini WillianAgostini force-pushed the feat/958 branch 2 times, most recently from 873341d to 155a77c Compare February 20, 2025 22:30
@sheerlox sheerlox requested a review from intcreator February 21, 2025 15:11
@sheerlox
Copy link
Collaborator

Works for me, thanks for the PR! @intcreator what's your take on adding this feature?

@sheerlox
Copy link
Collaborator

Since the parsing function returns the reason why a cron time would be invalid, it could be interesting to find a way to return the error.

@WillianAgostini
Copy link
Contributor Author

WillianAgostini commented Feb 21, 2025

Good point! I see two possible approaches here:

  1. Returning an object with valid and error and renaming function to validateCronExpression:
{
  valid: boolean;
  error?: Error;
}
  1. Using a negative approach where the function returns null if valid expression and an error object otherwise and renaming the function to isCronExpressionInvalid:
const error = isCronExpressionInvalid('not valid');
if (error) {
  // Handle the error
}

Which one do you think fits best with the project's style?

@sheerlox
Copy link
Collaborator

sheerlox commented Feb 21, 2025

I think the first approach with a function named validateCronExpression works best. Since the _parse function is private, this should be added as a static function to CronTime and re-exported from index.ts.

P.S. probably wait for @intcreator's feedback just in case!

@intcreator
Copy link
Collaborator

I agree with that approach @sheerlox

@sheerlox sheerlox self-requested a review February 23, 2025 10:52
Copy link
Collaborator

@sheerlox sheerlox left a comment

Choose a reason for hiding this comment

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

Apart from the test comment, LGTM!

Copy link
Collaborator

@sheerlox sheerlox left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution ❤️

@sheerlox sheerlox merged commit cbd8106 into kelektiv:main Feb 24, 2025
18 checks passed
@sheerlox
Copy link
Collaborator

oops, merged with the wrong function name in the commit message 😆

@ncb000gt
Copy link
Member

🎉 This PR is included in version 4.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants