forked from plusone/plusone-validator
Compare commits
6 Commits
25ec5577cc
...
875150aa6e
Author | SHA1 | Date | |
---|---|---|---|
875150aa6e | |||
75c24558cd | |||
6da1dfbceb | |||
7d29027b91 | |||
838e80eac5 | |||
5bb6351c5b |
@@ -8,5 +8,5 @@ indent_style = space
|
||||
indent_size = 4
|
||||
end_of_line = lf
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = false
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
2
pom.xml
2
pom.xml
@@ -21,7 +21,7 @@
|
||||
<dependency>
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
<artifactId>plusone-commons</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<version>1.0.0-RC3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
|
@@ -8,7 +8,20 @@ import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class BaseValidator<T> {
|
||||
/**
|
||||
* BaseValidator
|
||||
*
|
||||
* <p>
|
||||
* 校验器的基类
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* <b>NOTE: content.</b>
|
||||
* </p>
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public abstract class BaseValidator<T> {
|
||||
private final List<Consumer<? super T>> rules = new ArrayList<>();
|
||||
|
||||
protected void withRule(final Predicate<T> rule, final String errorMessage) {
|
||||
|
@@ -5,7 +5,7 @@ package xyz.zhouxy.plusone.validator;
|
||||
*
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
*
|
||||
* @see ValidateUtil
|
||||
* @see ValidTools
|
||||
* @see BaseValidator
|
||||
*/
|
||||
public interface IValidateRequired {
|
||||
|
@@ -6,22 +6,29 @@ import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
import xyz.zhouxy.plusone.commons.util.RegexTools;
|
||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
|
||||
/**
|
||||
* StringValidator
|
||||
*
|
||||
* <p>
|
||||
* 针对文本字段的验证器。
|
||||
* </p>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
*/
|
||||
public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, StringValidator<DTO>> {
|
||||
|
||||
StringValidator(Function<DTO, String> getter) {
|
||||
super(getter);
|
||||
}
|
||||
|
||||
// ====================
|
||||
// ====== String ======
|
||||
// ====================
|
||||
|
||||
// ===== matches =====
|
||||
// ================================
|
||||
// #region - matches
|
||||
// ================================
|
||||
|
||||
public StringValidator<DTO> matches(Pattern regex, String errMsg) {
|
||||
return matches(regex, convertExceptionCreator(errMsg));
|
||||
@@ -40,7 +47,13 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
||||
return this;
|
||||
}
|
||||
|
||||
// ===== matchesOne =====
|
||||
// ================================
|
||||
// #endregion - matches
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - matchesOne
|
||||
// ================================
|
||||
|
||||
public StringValidator<DTO> matchesOne(Pattern[] regexs, String errMsg) {
|
||||
return matchesOne(regexs, convertExceptionCreator(errMsg));
|
||||
@@ -76,7 +89,13 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
||||
return this;
|
||||
}
|
||||
|
||||
// ===== matchesAll =====
|
||||
// ================================
|
||||
// #endregion - matchesOne
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - matchesAll
|
||||
// ================================
|
||||
|
||||
public StringValidator<DTO> matchesAll(Pattern[] regexs, String errMsg) {
|
||||
return matchesAll(regexs, convertExceptionCreator(errMsg));
|
||||
@@ -112,19 +131,13 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
||||
return this;
|
||||
}
|
||||
|
||||
// ===== notBlank =====
|
||||
// ================================
|
||||
// #endregion - matchesAll
|
||||
// ================================
|
||||
|
||||
static boolean isNotBlank(final String cs) {
|
||||
if (cs == null || cs.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < cs.length(); i++) {
|
||||
if (!Character.isWhitespace(cs.charAt(i))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// ================================
|
||||
// #region - notBlank
|
||||
// ================================
|
||||
|
||||
public StringValidator<DTO> notBlank() {
|
||||
return notBlank("This String argument must have text; it must not be null, empty, or blank");
|
||||
@@ -140,11 +153,17 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> notBlank(
|
||||
Function<String, E> exceptionCreator) {
|
||||
withRule(StringValidator::isNotBlank, exceptionCreator);
|
||||
withRule(StringTools::isNotBlank, exceptionCreator);
|
||||
return this;
|
||||
}
|
||||
|
||||
// ===== email =====
|
||||
// ================================
|
||||
// #endregion - notBlank
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - email
|
||||
// ================================
|
||||
|
||||
public StringValidator<DTO> email() {
|
||||
return email("The value is not an email address.");
|
||||
@@ -159,10 +178,17 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> email(Function<String, E> exceptionCreator) {
|
||||
// TODO [优化] 优化 email 校验
|
||||
return matches(PatternConsts.EMAIL, exceptionCreator);
|
||||
}
|
||||
|
||||
// ====== notEmpty =====
|
||||
// ================================
|
||||
// #endregion - email
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - notEmpty
|
||||
// ================================
|
||||
|
||||
public StringValidator<DTO> notEmpty(String errMsg) {
|
||||
return notEmpty(convertExceptionCreator(errMsg));
|
||||
@@ -178,7 +204,13 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
||||
return this;
|
||||
}
|
||||
|
||||
// ====== isNullOrEmpty =====
|
||||
// ================================
|
||||
// #endregion - notEmpty
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - isNullOrEmpty
|
||||
// ================================
|
||||
|
||||
public StringValidator<DTO> isNullOrEmpty(String errMsg) {
|
||||
return isNullOrEmpty(convertExceptionCreator(errMsg));
|
||||
@@ -194,7 +226,13 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
||||
return this;
|
||||
}
|
||||
|
||||
// ====== length =====
|
||||
// ================================
|
||||
// #endregion - isNullOrEmpty
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - length
|
||||
// ================================
|
||||
|
||||
public StringValidator<DTO> length(int length, String errMsg) {
|
||||
return length(length, convertExceptionCreator(errMsg));
|
||||
@@ -207,7 +245,7 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> length(int length,
|
||||
Function<String, E> exceptionCreator) {
|
||||
Preconditions.checkArgument(length >= 0, "The minimum value must be less than the maximum value.");
|
||||
AssertTools.checkArgument(length >= 0, "The minimum value must be less than the maximum value.");
|
||||
withRule(s -> s != null && s.length() == length, exceptionCreator);
|
||||
return this;
|
||||
}
|
||||
@@ -231,12 +269,16 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> length(int min, int max,
|
||||
Function<String, E> exceptionCreator) {
|
||||
Preconditions.checkArgument(min >= 0, "The minimum value must be greater than equal to 0.");
|
||||
Preconditions.checkArgument(min < max, "The minimum value must be less than the maximum value.");
|
||||
AssertTools.checkArgument(min >= 0, "The minimum value must be greater than equal to 0.");
|
||||
AssertTools.checkArgument(min < max, "The minimum value must be less than the maximum value.");
|
||||
withRule(s -> length(s, min, max), exceptionCreator);
|
||||
return this;
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - length
|
||||
// ================================
|
||||
|
||||
@Override
|
||||
protected StringValidator<DTO> thisObject() {
|
||||
return this;
|
||||
|
@@ -12,8 +12,8 @@ package xyz.zhouxy.plusone.validator;
|
||||
* @see Validator
|
||||
* @see IValidateRequired
|
||||
*/
|
||||
public class ValidateUtil {
|
||||
private ValidateUtil() {
|
||||
public class ValidTools {
|
||||
private ValidTools() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
@@ -20,7 +20,7 @@ import java.util.function.Supplier;
|
||||
*
|
||||
* <p>
|
||||
* 然后通过校验器的 {@link #validate} 方法,或
|
||||
* {@link ValidateUtil#validate(Object, Validator)} 对指定对象进行校验。
|
||||
* {@link ValidTools#validate(Object, Validator)} 对指定对象进行校验。
|
||||
* </p>
|
||||
*
|
||||
* <pre>
|
||||
@@ -28,13 +28,13 @@ import java.util.function.Supplier;
|
||||
* </pre>
|
||||
*
|
||||
* <pre>
|
||||
* ValidateUtil.validate(255, validator);
|
||||
* ValidTools.validate(255, validator);
|
||||
* </pre>
|
||||
* </p>
|
||||
*
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
* @see IValidateRequired
|
||||
* @see ValidateUtil
|
||||
* @see ValidTools
|
||||
* @see BaseValidator
|
||||
*/
|
||||
public final class Validator<T> extends BaseValidator<T> {
|
||||
|
@@ -28,10 +28,15 @@ class MapValidatorTests {
|
||||
params.put(ParamsValidator.PASSWORD2, "99Code108");
|
||||
params.put(ParamsValidator.AGE, 18);
|
||||
params.put(ParamsValidator.BOOLEAN, true);
|
||||
params.put(ParamsValidator.ROLE_LIST, Arrays.asList("admin", ""));
|
||||
|
||||
Map<String, Object> validedParams = validator.validateAndCopy(params);
|
||||
System.out.println(validedParams);
|
||||
params.put(ParamsValidator.ROLE_LIST, Arrays.asList("admin", ""));
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
validator.validateAndCopy(params);
|
||||
});
|
||||
|
||||
params.put(ParamsValidator.ROLE_LIST, Arrays.asList("admin", "developer"));
|
||||
Map<String, Object> validatedParams = validator.validateAndCopy(params);
|
||||
System.out.println(validatedParams);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -11,7 +11,7 @@ import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||
import xyz.zhouxy.plusone.commons.function.PredicateTools;
|
||||
import xyz.zhouxy.plusone.commons.util.RegexTools;
|
||||
import xyz.zhouxy.plusone.validator.BaseValidator;
|
||||
import xyz.zhouxy.plusone.validator.ValidateUtil;
|
||||
import xyz.zhouxy.plusone.validator.ValidTools;
|
||||
|
||||
class BaseValidatorTest {
|
||||
@Test
|
||||
@@ -21,7 +21,7 @@ class BaseValidatorTest {
|
||||
"A1b2C3d4",
|
||||
Arrays.asList(new String[] { "admin", "editor" }));
|
||||
RegisterCommandValidator.INSTANCE.validate(registerCommand);
|
||||
ValidateUtil.validate(registerCommand, RegisterCommandValidator.INSTANCE);
|
||||
ValidTools.validate(registerCommand, RegisterCommandValidator.INSTANCE);
|
||||
System.out.println(registerCommand);
|
||||
}
|
||||
|
||||
|
@@ -10,10 +10,9 @@ import java.util.regex.Pattern;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||
import xyz.zhouxy.plusone.commons.function.PredicateTools;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
import xyz.zhouxy.plusone.commons.util.RegexTools;
|
||||
import xyz.zhouxy.plusone.validator.Validator;
|
||||
|
||||
@@ -43,14 +42,14 @@ class ValidatorTests {
|
||||
// 传入 rule
|
||||
.addRule(command -> {
|
||||
String code = command.getCode();
|
||||
Preconditions.checkArgument(Objects.nonNull(code), "验证码不能为空");
|
||||
Preconditions.checkArgument(RegexTools.matches(code, CAPTCHA), "验证码不符合规范");
|
||||
AssertTools.checkArgument(Objects.nonNull(code), "验证码不能为空");
|
||||
AssertTools.checkArgument(RegexTools.matches(code, CAPTCHA), "验证码不符合规范");
|
||||
})
|
||||
// 传入 rule
|
||||
.addRule(command -> {
|
||||
String password = command.getPassword();
|
||||
Preconditions.checkArgument(StringUtils.isNotEmpty(password), "密码不能为空");
|
||||
Preconditions.checkArgument(RegexTools.matches(password, PASSWORD), "密码不符合规范");
|
||||
AssertTools.checkArgument(StringUtils.isNotEmpty(password), "密码不能为空");
|
||||
AssertTools.checkArgument(RegexTools.matches(password, PASSWORD), "密码不符合规范");
|
||||
})
|
||||
// 传入 predicate 和 Supplier<E extends RuntimeException>
|
||||
.addRule(command -> CollectionTools.isNotEmpty(command.getRoles()),
|
||||
|
Reference in New Issue
Block a user