com.bixuebihui.r2dbc.db.MaxByteLengthValidator Maven / Gradle / Ivy
The newest version!
package com.bixuebihui.r2dbc.db;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.constraints.Size;
import java.io.UnsupportedEncodingException;
/**
* see 数据库字符串长度校验
* 如果数据库用的是utf8mb4则长度会限制更短。
*
* @author xingwx
* @version $Id: $Id
*/
public class MaxByteLengthValidator implements ConstraintValidator {
private static String UTF_8="UTF-8";
private static final Logger LOG = LoggerFactory.getLogger(MaxByteLengthValidator.class);
private int max;
/**
* initialize.
*
* @param constraintAnnotation a {@link Size} object.
*/
@Override
public void initialize(Size constraintAnnotation) {
this.max = constraintAnnotation.max();
}
/**
* {@inheritDoc}
*/
@Override
public boolean isValid(String object, ConstraintValidatorContext constraintContext) {
try {
return object == null || object.getBytes(UTF_8).length <= this.max;
} catch (UnsupportedEncodingException e) {
LOG.error("isValid", e);
}
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy