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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

WillianAgostini
Copy link

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).

@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
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
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