Pages

Friday, April 28, 2023

.Net Cron Validation

 

The code you shared is a C# implementation of a helper class for validating cron expressions. Cron expressions are used to schedule tasks to run at specific times or intervals, and are commonly used in Unix-based operating systems.

The CronHelper class provides a static method IsValidCron() which takes a cron expression as input and returns a boolean indicating whether the expression is valid. The class uses an array of tuples cronPartIntervals to define the valid range of values for each part of the cron expression (minute, hour, day of month, month, and day of week).

The IsValidCronPart() method is used to validate each part of the cron expression individually. It supports single values, ranges, and step values (e.g. "*/5" to run every 5 minutes).

By using this helper class, you can ensure that the cron expressions provided by users are valid before using them to schedule tasks, which can help prevent errors and improve the reliability of your application.

using System;
using System.Text.RegularExpressions;

namespace AppCore.Cron
{
    public static class CronHelper
    {
        readonly static Tuple<int, int>[] cronPartIntervals = new Tuple<int, int>[5]
        {
            new Tuple<int, int>(0, 59), //minute
            new Tuple<int, int>(0, 23), //hour
            new Tuple<int, int>(1, 31), //day of month
            new Tuple<int, int>(1, 12), //month
            new Tuple<int, int>(0, 6)   //day of week
        };

        public static bool IsValidCron(string cron)
        {
            if (string.IsNullOrEmpty(cron))
                return false;
           

            var parts = cron.Split(' ');
            if (parts.Length != cronPartIntervals.Length)
                return false;

            for (int i = 0; i < parts.Length; i++)
            {
                if (!IsValidCronPart(parts[i], cronPartIntervals[i].Item1, cronPartIntervals[i].Item2))
                    return false;
            }


            return true;
        }

        private static bool IsValidCronPart(string part, int minValue, int maxValue)
        {
            if (part == "*")
                return true;

            var values = part.Split(',');
            foreach (string value in values)
            {
                if (value.StartsWith("*/")) // step value
                {
                    if (!int.TryParse(value.Substring(2), out int step))
                        return false;

                    if (step <= 0 || step > maxValue || step < minValue)
                        return false;
                }
                else
                {
                    var range = value.Split('-');
                    if (range.Length == 2) // range value
                    {
                        if (!int.TryParse(range[0], out int start) || !int.TryParse(range[1], out int end))
                            return false;

                        if (start < minValue || start > maxValue || end < minValue || end > maxValue || start > end)
                            return false;
                    }
                    else // single value
                    {
                        if (!int.TryParse(value, out int single))
                            return false;

                        if (single < minValue || single > maxValue)
                            return false;
                    }
                }
            }

            return true;
        }
    }
}

No comments:

Post a Comment