`
吃猫的鱼
  • 浏览: 44062 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

Cron表达式校验

阅读更多
function CronExpressionValidator() {   
}   
  
CronExpressionValidator.validateCronExpression = function(value) {   
    var results = true;   
    if (value == null || value.length == 0) {   
        return false;   
    }   
  
    // split and test length   
    var expressionArray = value.split(" ");   
    var len = expressionArray.length;   
  
    if ((len != 6) && (len != 7)) {   
        return false;   
    }   
  
    // check only one question mark   
    var match = value.match(/\?/g);   
    if (match != null && match.length > 1) {   
        return false;   
    }   
  
    // check only one question mark   
    var dayOfTheMonthWildcard = "";   
  
    // if appropriate length test parts   
    // [0] Seconds 0-59 , - * /   
    if (CronExpressionValidator.isNotWildCard(expressionArray[0], /[\*]/gi)) {   
        if (!CronExpressionValidator.segmentValidator("([0-9\\\\,-\\/])",   
                expressionArray[0], [0, 59], "seconds")) {   
            return false;   
        }   
    }   
  
    // [1] Minutes 0-59 , - * /   
    if (CronExpressionValidator.isNotWildCard(expressionArray[1], /[\*]/gi)) {   
        if (!CronExpressionValidator.segmentValidator("([0-9\\\\,-\\/])",   
                expressionArray[1], [0, 59], "minutes")) {   
            return false;   
        }   
    }   
  
    // [2] Hours 0-23 , - * /   
    if (CronExpressionValidator.isNotWildCard(expressionArray[2], /[\*]/gi)) {   
        if (!CronExpressionValidator.segmentValidator("([0-9\\\\,-\\/])",   
                expressionArray[2], [0, 23], "hours")) {   
            return false;   
        }   
    }   
  
    // [3] Day of month 1-31 , - * ? / L W C   
    if (CronExpressionValidator.isNotWildCard(expressionArray[3], /[\*\?]/gi)) {   
        if (!CronExpressionValidator.segmentValidator("([0-9LWC\\\\,-\\/])",   
                expressionArray[3], [1, 31], "days of the month")) {   
            return false;   
        }   
    } else {   
        dayOfTheMonthWildcard = expressionArray[3];   
    }   
  
    // [4] Month 1-12 or JAN-DEC , - * /   
    if (CronExpressionValidator.isNotWildCard(expressionArray[4], /[\*]/gi)) {   
        expressionArray[4] = CronExpressionValidator   
                .convertMonthsToInteger(expressionArray[4]);   
        if (!CronExpressionValidator.segmentValidator("([0-9\\\\,-\\/])",   
                expressionArray[4], [1, 12], "months")) {   
            return false;   
        }   
    }   
  
    // [5] Day of week 1-7 or SUN-SAT , - * ? / L C #   
    if (CronExpressionValidator.isNotWildCard(expressionArray[5], /[\*\?]/gi)) {   
        expressionArray[5] = CronExpressionValidator   
                .convertDaysToInteger(expressionArray[5]);   
        if (!CronExpressionValidator.segmentValidator("([0-9LC#\\\\,-\\/])",   
                expressionArray[5], [1, 7], "days of the week")) {   
            return false;   
        }   
    } else {   
        if (dayOfTheMonthWildcard == String(expressionArray[5])) {   
            // results.push(new ValidationResult(true, baseField, "wrongFormat",   
            // validator.wrongFormatError));   
            return false;   
        }   
    }   
  
    // [6] Year empty or 1970-2099 , - * /   
    if (len == 7) {   
        if (CronExpressionValidator.isNotWildCard(expressionArray[6], /[\*]/gi)) {   
            if (!CronExpressionValidator.segmentValidator("([0-9\\\\,-\\/])",   
                    expressionArray[6], [1970, 2099], "years")) {   
                return false;   
            }   
        }   
    }   
    return true;   
}   
  
// ----------------------------------   
// isNotWildcard 静态方法;   
// ----------------------------------   
CronExpressionValidator.isNotWildCard = function(value, expression) {   
    var match = value.match(expression);   
    return (match == null || match.length == 0) ? true : false;   
}   
  
// ----------------------------------   
// convertDays 静态方法;   
// ----------------------------------   
CronExpressionValidator.convertDaysToInteger = function(value) {   
    var v = value;   
    v = v.replace(/SUN/gi, "1");   
    v = v.replace(/MON/gi, "2");   
    v = v.replace(/TUE/gi, "3");   
    v = v.replace(/WED/gi, "4");   
    v = v.replace(/THU/gi, "5");   
    v = v.replace(/FRI/gi, "6");   
    v = v.replace(/SAT/gi, "7");   
    return v;   
}   
  
CronExpressionValidator.convertMonthsToInteger = function(value) {   
    var v = value;   
    v = v.replace(/JAN/gi, "1");   
    v = v.replace(/FEB/gi, "2");   
    v = v.replace(/MAR/gi, "3");   
    v = v.replace(/APR/gi, "4");   
    v = v.replace(/MAY/gi, "5");   
    v = v.replace(/JUN/gi, "6");   
    v = v.replace(/JUL/gi, "7");   
    v = v.replace(/AUG/gi, "8");   
    v = v.replace(/SEP/gi, "9");   
    v = v.replace(/OCT/gi, "10");   
    v = v.replace(/NOV/gi, "11");   
    v = v.replace(/DEC/gi, "12");   
    return v;   
}   
  
// ----------------------------------   
// segmentValidator 静态方法;   
// ----------------------------------   
CronExpressionValidator.segmentValidator = function(expression, value, range,   
        segmentName) {   
    var v = value;   
    var numbers = new Array();   
  
    // first, check for any improper segments   
    var reg = new RegExp(expression, "gi");   
    if (!reg.test(v)) {   
        //alert("reg[" + expression + "] " + v + " is not valid expression!");   
        return false;   
    }   
  
    // check duplicate types   
    // check only one L   
    var dupMatch = value.match(/L/gi);   
    if (dupMatch != null && dupMatch.length > 1) {   
        return false;   
    }   
  
    // look through the segments   
    // break up segments on ','   
    // check for special cases L,W,C,/,#,-   
    var split = v.split(",");   
    var i = -1;   
    var l = split.length;   
    var match;   
  
    while (++i < l) {   
        // set vars   
        var checkSegment = split[i];   
        var n;   
        var pattern = /(\w*)/;   
        match = pattern.exec(checkSegment);   
  
        // if just number   
        pattern = /(\w*)\-?\d+(\w*)/;   
        match = pattern.exec(checkSegment);   
  
        if (match   
                && match[0] == checkSegment   
                && checkSegment.indexOf("L") == -1   
                && checkSegment.indexOf("l") == -1   
                && checkSegment.indexOf("C") == -1   
                && checkSegment.indexOf("c") == -1   
                && checkSegment.indexOf("W") == -1   
                && checkSegment.indexOf("w") == -1   
                && checkSegment.indexOf("/") == -1   
                && (checkSegment.indexOf("-") == -1 || checkSegment   
                        .indexOf("-") == 0) && checkSegment.indexOf("#") == -1) {   
            n = match[0];   
  
            if (n && !(isNaN(n)))   
                numbers.push(n);   
            else if (match[0] == "0")   
                numbers.push(n);   
            continue;   
        }   
// includes L, C, or w   
        pattern = /(\w*)L|C|W(\w*)/i;   
        match = pattern.exec(checkSegment);   
  
        if (match   
                && match[0] != ""  
                && (checkSegment.indexOf("L") > -1   
                        || checkSegment.indexOf("l") > -1   
                        || checkSegment.indexOf("C") > -1   
                        || checkSegment.indexOf("c") > -1   
                        || checkSegment.indexOf("W") > -1 || checkSegment   
                        .indexOf("w") > -1)) {   
  
            // check just l or L   
            if (checkSegment == "L" || checkSegment == "l")   
                continue;   
            pattern = /(\w*)\d+(l|c|w)?(\w*)/i;   
            match = pattern.exec(checkSegment);   
  
            // if something before or after   
            if (!match || match[0] != checkSegment) {   
                // results.push(new ValidationResult(true, null, "noMatch", "The   
                // " + segmentName + " segment is invalid."));   
                continue;   
            }   
  
            // get the number   
            var numCheck = match[0];   
            numCheck = numCheck.replace(/(l|c|w)/ig, "");   
  
            n = Number(numCheck);   
  
            if (n && !(isNaN(n)))   
                numbers.push(n);   
            else if (match[0] == "0")   
                numbers.push(n);   
            continue;   
        }   
  
        var numberSplit;   
  
        // includes /   
        if (checkSegment.indexOf("/") > -1) {   
            // take first #   
            numberSplit = checkSegment.split("/");   
  
            if (numberSplit.length != 2) {   
                // results.push(new ValidationResult(true, null, "noMatch", "The   
                // " + segmentName + " segment is invalid."));   
                continue;   
            } else {   
                n = numberSplit[0];   
  
                if (n && !(isNaN(n)))   
                    numbers.push(n);   
                else if (numberSplit[0] == "0")   
                    numbers.push(n);   
                continue;   
            }   
        }   
  
        // includes #   
        if (checkSegment.indexOf("#") > -1) {   
            // take first #   
            numberSplit = checkSegment.split("#");   
  
            if (numberSplit.length != 2) {   
                // results.push(new ValidationResult(true, null, "noMatch", "The   
                // " + segmentName + " segment is invalid."));   
                continue;   
            } else {   
                n = numberSplit[0];   
  
                if (n && !(isNaN(n)))   
                    numbers.push(n);   
                else if (numberSplit[0] == "0")   
                    numbers.push(n);   
                continue;   
            }   
  
// includes -   
        if (checkSegment.indexOf("-") > 0) {   
            // take both #   
            numberSplit = checkSegment.split("-");   
  
            if (numberSplit.length != 2) {   
                // results.push(new ValidationResult(true, null, "noMatch", "The   
                // " + segmentName + " segment is invalid."));   
                continue;   
            } else if (Number(numberSplit[0]) > Number(numberSplit[1])) {   
                // results.push(new ValidationResult(true, null, "noMatch", "The   
                // " + segmentName + " segment is invalid."));   
                continue;   
            } else {   
                n = numberSplit[0];   
  
                if (n && !(isNaN(n)))   
                    numbers.push(n);   
                else if (numberSplit[0] == "0")   
                    numbers.push(n);   
                n = numberSplit[1];   
  
                if (n && !(isNaN(n)))   
                    numbers.push(n);   
                else if (numberSplit[1] == "0")   
                    numbers.push(n);   
                continue;   
            }   
        }   
  
    }   
    // lastly, check that all the found numbers are in range   
    i = -1;   
    l = numbers.length;   
  
    if (l == 0)   
        return false;   
  
    while (++i < l) {   
        // alert(numbers[i]);   
        if (numbers[i] < range[0] || numbers[i] > range[1]) {   
            //alert(numbers[i] + ' is not in the range [' + range[0] + ", "   
            //      + range[1] + "]");   
            return false;   
        }   
    }   
    return true;   
}   
  
  
function cronValidate(cronExpression) {   
    return CronExpressionValidator.validateCronExpression(cronExpression);   
}   
        }  

分享到:
评论

相关推荐

    cron表达式校验,验证是否是正确的cron表达式,调用的主方法是function cronValidate(cronExpre

    cron表达式校验,验证是否是正确的cron表达式,调用的主方法是function cronValidate(cronExpression )

    详细简单的cron表达式校验js

    cron表达式校验,验证是否是正确的cron表达式,调用的主方法是function cronValidate(cronExpression ),有需要可以看一下

    Cron表达式验证工具

    Cron表达式写好了,不知道是否正确,如果执行间隔很长,根本没法测试,这个工具可以帮助你,他能输出所有的执行时间,你只需自己调整参数就行了,非常简单。提醒:目前常用的两个Cron表达式在线生成网站所提供的近期...

    cron-validator:验证cron表达式

    Cron验证器Cron Validator是一种实用程序,可让您在代码库中验证cron表达式,类似于所做的事情。备择方案 :它具有更多功能和配置选项来限制范围或创建配置预设。 它包含一个应该与AWS Schedule Expressions相匹配的...

    cronmatch:cronmatch检查日期时间是否与cron表达式匹配

    cronmatch cronmatch检查日期/时间是否与cron表达式匹配。用法match方法接受两个参数: cron表达式日期(或可以转换为日期的内容) const cronmatch = require ( 'cronmatch' )// If the minute of the current time...

    react-cron-generator:简单的react组件以生成cron表达式(材料UI)

    React时间发生器简单的react组件以生成cron表达式(Material-UI)入门软件包有助于构建Linux Scheduler cron表达式。 该项目使用material-ui data = '* * * * * * *'npm i --save @dealmeddevs/react-cron-generator...

    Cron expression 校验 js版本

    NULL 博文链接:https://wv1124.iteye.com/blog/503928

    Cronos:功能齐全的.NET库,用于处理Cron表达式。 牢记时区,直观地处理夏时制转换

    Cronos是一个.NET库,用于解析Cron表达式并计算下一次出现。 它是设计时考虑时区,并直观地处理(又名夏令时)转换为* nix中的Cron。 请注意,该库不包含任何任务/作业计划程序,仅适用于Cron表达式。 支持带有可...

    react-cron:基于react和antd完成的一个生成cron表达式的插件

    React时间概述基于react和antd完成的一个创建cron的插件。核心代码在src/cron/index.js。本地安装引用 import ReactCron from '@/cron/index.js' require ( "@/cron/index.css) class App extends React.Component {...

    详解Javascript判断Crontab表达式是否合法

    主要介绍了详解Javascript判断Crontab表达式是否合法的相关资料,需要的朋友可以参考下

    Guns-Separation-其他

    16、定时任务:定时任务的维护,通过cron表达式控制任务的执行频率。17、系统配置:系统运行的参数的维护,参数的配置与系统运行机制息息相关。18、邮件发送:发送邮件功能。19、短信发送:短信发送功能,可使用阿里...

    Guns-Separation v1.1

    16、定时任务:定时任务的维护,通过cron表达式控制任务的执行频率。 17、系统配置:系统运行的参数的维护,参数的配置与系统运行机制息息相关。 18、邮件发送:发送邮件功能。 19、短信发送:短信发送功能,可...

    单点登录源码

    FluentValidator | 校验框架 | [https://github.com/neoremind/fluent-validator](https://github.com/neoremind/fluent-validator) Thymeleaf | 模板引擎 | [http://www.thymeleaf.org/](http://www.thymeleaf.org/...

Global site tag (gtag.js) - Google Analytics