CronExpressionValidator.java

  1. /*
  2.  * Copyright 2024 Global Crop Diversity Trust
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */

  16. package org.gringlobal.custom.validation.javax;

  17. import org.springframework.scheduling.support.CronExpression;

  18. import javax.validation.ConstraintValidator;
  19. import javax.validation.ConstraintValidatorContext;

  20. public class CronExpressionValidator implements ConstraintValidator<CronExpressionValidation, String> {
  21.    
  22.     @Override
  23.     public boolean isValid(String value, ConstraintValidatorContext context) {
  24.         if (value == null) {
  25.             return true;
  26.         }
  27.         return CronExpression.isValidExpression(value);
  28.     }
  29. }