Skip to content

trossr32/aws-schedule-expression-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AWS Schedule Expression Validator

NuGet
GitHub Packages
CI Publish NuGet Package Publish DotNet Tool Publish GitHub Package Publish Powershell Module Deploy GitHub Pages

Validate AWS EventBridge Scheduler expressions (cron, rate, and at) and optionally enforce minimum/maximum intervals between runs. Includes FluentValidation integrations and a Blazor WebAssembly demo.

Features

  • Validate cron(...), rate(...), and at(...) expressions
  • Accepts bare CRON fields for backward compatibility
  • Validate interval constraints (min/max) between occurrences
  • Retrieve upcoming execution times
  • Fluent API
  • FluentValidation rule extensions
  • Blazor WebAssembly demo deployed to GitHub Pages
  • Packages/Tools published to NuGet.org and GitHub Packages
  • PowerShell module published to PowerShell Gallery

NuGet Package

dotnet add package AwsScheduleExpressionValidator

Usage

Fluent validation API

using AwsScheduleExpressionValidator;

var result = "rate(5 minutes)"
    .ValidateAwsScheduleExpression()
    .WithMinInterval(TimeSpan.FromMinutes(1))
    .WithMaxInterval(TimeSpan.FromMinutes(10))
    .Evaluate();

var isValid = "cron(0 10 * * ? *)"
    .ValidateAwsScheduleExpression()
    .IsValid();

var occurrences = "rate(1 minutes)"
    .ValidateAwsScheduleExpression()
    .WithOccurrenceCount(3)
    .WithOccurrenceStart(DateTimeOffset.UtcNow)
    .GetNextScheduleExpressionOccurrences();

"invalid"
    .ValidateAwsScheduleExpression()
    .ThrowExceptionOnFailure()
    .Evaluate();

Fluent API reference

  • ValidateAwsScheduleExpression()
    • Starts validation for a schedule expression string.
  • WithMinInterval(TimeSpan)
    • Sets a minimum interval constraint.
  • WithMaxInterval(TimeSpan)
    • Sets a maximum interval constraint.
  • WithOccurrenceCount(int)
    • Sets the number of occurrences to return for GetNextScheduleExpressionOccurrences().
  • WithOccurrenceStart(DateTimeOffset)
    • Sets the starting point for occurrence calculation.
  • ThrowExceptionOnFailure()
    • Throws AwsScheduleExpressionFormatException, AwsScheduleIntervalConfigurationException, or AwsScheduleIntervalViolationException when validation fails.
  • IsValid()
    • Returns true/false for the current configuration. Use this or Evaluate() to get validation results.
  • Evaluate()
    • Returns AwsScheduleExpressionValidationResult with IsValid, Error, and Message. Use this or IsValid() to get validation results.
  • GetNextScheduleExpressionOccurrences()
    • Returns upcoming execution times using the configured occurrence settings.

Format validation

using AwsScheduleExpressionValidator;

// Valid CRON expression
var isValid = AwsScheduleExpressionValidator.ValidateFormat("cron(0 10 * * ? *)");
var isValid = AwsScheduleExpressionValidator.ValidateFormat("0 10 * * ? *");

// Valid rate expression
var isValid = AwsScheduleExpressionValidator.ValidateFormat("rate(5 minutes)");

// Valid at expression
var isValid = AwsScheduleExpressionValidator.ValidateFormat("at(2024-12-31T23:59:00)");

Validate with interval constraints

using AwsScheduleExpressionValidator;

var minInterval = TimeSpan.FromHours(1);
var maxInterval = TimeSpan.FromDays(1);

var isValid = AwsScheduleExpressionValidator.ValidateWithIntervals("rate(5 minutes)", minInterval, maxInterval);

Get upcoming executions

using AwsScheduleExpressionValidator;

var nextRuns = AwsScheduleExpressionValidator.GetNextOccurrences("cron(0 10 * * ? *)", count: 5);

foreach (var run in nextRuns)
{
    Console.WriteLine(run.ToLocalTime().ToString("f"));
}

FluentValidation integration

using AwsScheduleExpressionValidator;
using FluentValidation;

public class ScheduleRequest
{
    public string Expression { get; set; } = string.Empty;
}

public class ScheduleRequestValidator : AbstractValidator<ScheduleRequest>
{
    public ScheduleRequestValidator()
    {
        // Basic format validation
        RuleFor(x => x.Expression)
            .MustBeValidAwsSchedule();

        // Validate with a minimum interval of 5 minutes
        RuleFor(x => x.Expression)
            .MustBeValidAwsSchedule(TimeSpan.FromMinutes(5));
            
        // Validate with a maximum interval of 24 hours
        RuleFor(x => x.Expression)
            .MustBeValidAwsSchedule(null, TimeSpan.FromHours(24));
            
        // Validate with a minimum interval of 5 minutes and a maximum interval of 24 hours
        RuleFor(x => x.Expression)
            .MustBeValidAwsSchedule(TimeSpan.FromMinutes(5), TimeSpan.FromHours(24));
    }
}

CLI Tool

Install the .NET tool:

dotnet tool install -g AwsScheduleExpressionValidator.Tool

Examples:

aws-schedule-expression-validator format "rate(5 minutes)"
aws-schedule-expression-validator validate "cron(0 10 * * ? *)" --min 00:05:00 --max 01:00:00
aws-schedule-expression-validator occurrences "rate(5 minutes)" --count 3

Commands:

  • format <expression>: Validates the expression format only.
  • validate <expression> [--min <timespan>] [--max <timespan>]: Validates format and interval constraints.
  • occurrences <expression> [--count <number>] [--start <iso-8601>]: Lists upcoming occurrences.

Options:

  • --min: Minimum interval (TimeSpan, e.g. 00:05:00).
  • --max: Maximum interval (TimeSpan, e.g. 01:00:00).
  • --count: Number of occurrences to return (default 5).
  • --start: Start date/time (ISO 8601).

PowerShell module commands

Install the PowerShell module from the PowerShell Gallery and use the cmdlets to validate expressions or list occurrences.

Install-Module AwsScheduleExpressionValidator.PsModule -Scope CurrentUser
Import-Module AwsScheduleExpressionValidator.PsModule

Test-AwsScheduleExpressionFormat

Tests whether a schedule expression has a valid AWS format.

Test-AwsScheduleExpressionFormat -Expression 'rate(5 minutes)'

Test-AwsScheduleExpression

Validates a schedule expression and optionally enforces minimum and maximum interval constraints.

Test-AwsScheduleExpression -Expression 'rate(5 minutes)' -MinInterval '00:01:00' -MaxInterval '01:00:00'

Get-AwsScheduleExpressionOccurrence

Returns upcoming occurrences for a schedule expression, optionally starting at a specified time.

Get-AwsScheduleExpressionOccurrence -Expression 'rate(5 minutes)' -Count 3 -Start '2024-01-01T00:00:00Z'

Tests

Run the unit tests from the solution root:

dotnet test src/AwsScheduleExpressionValidator.Tests/AwsScheduleExpressionValidator.Tests.csproj

Development

The solution includes a Blazor WebAssembly demo (src/AwsScheduleExpressionValidatorDemo) deployed to GitHub Pages. To run locally:

dotnet run --project src/AwsScheduleExpressionValidatorDemo/AwsScheduleExpressionValidatorDemo.csproj

Notes

  • For at(...) expressions, interval constraints are not applicable (one-time schedules).
  • When no cron(...) prefix is provided, the validator assumes the input is a CRON expression.

About

Validate AWS Scheduler cron and rate expressions. Optionally validate intervals and integrate with FluentValidation. Available as a NuGet package, .NET tool and PowerShell module.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors