All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.codeinghelper.annotation.validator.LengthValidator Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.github.codeinghelper.annotation.validator;
import com.github.codeinghelper.util.StringUtil;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.text.MessageFormat;

/**
 * @BelongsProject: test
 * @BelongsPackage: com.example.demo.validator
 * @Author: lizeze
 * @CreateTime: 2020-08-04 22:37
 * @Description: ${Description}
 */
public class LengthValidator implements ConstraintValidator {

    int min;
    int max;
    String message = "";

    @Override
    public void initialize(Length constraintAnnotation) {
        max = constraintAnnotation.max();
        min = constraintAnnotation.min();
        message = MessageFormat.format(constraintAnnotation.message(), min, max);

    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {

        if (StringUtil.isEmptyOrNull(s)) return false;
        return s.length() >= min && s.length() <= max;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy