Newer
Older
validator / src / main / java / com / ljunggren / validator / validation / RegexValidationChain.java
@aljunggren aljunggren on 7 Sep 2020 996 bytes Changed errorMessage to list
package com.ljunggren.validator.validation;

import java.lang.annotation.Annotation;

import com.ljunggren.validator.Item;
import com.ljunggren.validator.annotation.RegexValidation;
import com.ljunggren.validator.evaluation.Evaluation;
import com.ljunggren.validator.evaluation.RegexEvaluation;

public class RegexValidationChain extends ValidationChain {

    private Class<?> annotationClass = RegexValidation.class;
    

    @Override
    public void validate(Annotation annotation, Item item) {
        if (annotation.annotationType() == annotationClass && item.getValue() instanceof String) {
            String regex = ((RegexValidation) annotation).regex();
            Evaluation<String> evaluation = new RegexEvaluation(regex);
            if (!evaluation.isValid(item.getValue().toString())) {
                item.addErrorMessage(evaluation.getErrorMessage());
                return;
            }
            return;
        }
        nextChain.validate(annotation, item);
    }

}