简体   繁体   English

在 WSO2 中验证 yyyy-MM-dd 格式的字符串日期

[英]Validate a String date with yyyy-MM-dd format in WSO2

I'm using wso2 MI 1.2.0.我正在使用 wso2 MI 1.2.0。 I need to validate a date that comes as a String with yyyy-MM-dd format.我需要验证以 yyyy-MM-dd 格式作为字符串出现的日期。 Need to consider the major validations of date and avoid the below kind of invalid dates.需要考虑日期的主要验证并避免以下类型的无效日期。

  • 2021-02-29 2021-02-29
  • 2021-03-32 2021-03-32
  • 2021-14-03 2021-14-03
  • 2021-1r-09 2021-1r-09
  • 2020-10-09t 2020-10-09t

I tried to use script mediator as mention in below.我尝试使用下面提到的脚本调解器。 But it's passing invalid date when trying to invoke new Date() function.但是在尝试调用new Date() function 时,它传递了无效的日期

<script language="js"><![CDATA[var myDate = mc.getProperty("myDate");
     var regEx = /^\d{4}-\d{2}-\d{2}$/;
     var isMyDateValid = 'false';
      if(isValidDate(myDate)) {
            isMyDateValid = 'true';
    }
            
    function isValidDate(dateString) {
        var log = mc.getServiceLog();        
        log.info("Logging inside Script Mediator"+dateString);
          var regEx = /^\d{4}-\d{2}-\d{2}$/;
          if(!dateString.match(regEx)) {
            return false;
          }
          var d = new Date(dateString);
          log.info("Logging inside Script Mediator d : "+d);
          var dNum = d.getTime();
          if(!dNum && dNum !== 0){
            return false;
          }
          return d.toISOString().slice(0,10) === dateString;
        }

    mc.setProperty("isMyDateValid", isMyDateValid);]]></script>

What can be the best ways to overcome this?克服这个问题的最佳方法是什么? Or any other alternative suggestions are also welcome.或者也欢迎任何其他替代建议。 Thank You in Advance!先感谢您!

The problem with WSO2 Script Mediator, is in that it does not run the script directly in the selected language, but wraps itself into Java and then tries to run. WSO2 Script Mediator 的问题在于它不会直接以所选语言运行脚本,而是将自身包装到 Java 中,然后尝试运行。 And that's why sometimes the JavaScript is not work as expected.这就是为什么有时 JavaScript 无法按预期工作的原因。 It's better to write there code in Java - yes, you can do that.最好在 Java 中编写代码 - 是的,你可以这样做。 For your validating problem you can use, something like below:对于您可以使用的验证问题,如下所示:

<script language="js"><![CDATA[
var myDate = mc.getProperty("myDate");
try {
  //Java 8 uses 'uuuu' for year, not 'yyyy'. In Java 8, ‘yyyy’ means “year of era” (BC or AD).
  var dateFormat = "uuuu-MM-dd";
  var dtf = java.time.format.DateTimeFormatter.ofPattern(dateFormat);
  java.time.LocalDate.parse(myDate, dtf.withResolverStyle(java.time.format.ResolverStyle.STRICT));
  mc.setProperty("isMyDateValid", true);
} catch (error) {
  mc.setProperty("isMyDateValid", false);
}      
]]></script>
function validateDate(str) {
  const date = new Date(str)
  const isValid = !isNaN(date.getTime())
  return isValid && date.toISOString().slice(0, 10) === str
}

function main() {
  console.log(validateDate('2021-02-29'))
  console.log(validateDate('2021-03-32'))
  console.log(validateDate('2021-14-03'))
  console.log(validateDate('2021-1r-09'))
  console.log(validateDate('2020-10-09t'))
  console.log(validateDate('2020-02-29'))
}
main()

outputs:输出:

false
false
false
false
false
true

 var invalidDates = `2021-02-29 2021-03-32 2021-14-03 2021-1r-09 2020-10-09t`.split('\n'); var validDates = `2020-02-29 2021-03-31 2021-02-03 2021-11-09 2020-10-09`.split('\n'); function isValidDate(dateString) { var d = new Date(dateString); if(isFinite(d.getTime())) { const [ year, month, date ] = dateString.split("-"); return d.getFullYear() == +year && (d.getMonth() + 1) == +month && d.getDate() == +date; } return false; } console.log(invalidDates.map(isValidDate)); console.log(validDates.map(isValidDate));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM