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