新增 ValidatorOfComparable。
This commit is contained in:
parent
011788b112
commit
e651b40aa0
@ -51,6 +51,12 @@ public abstract class BaseValidator<T> {
|
|||||||
return validator;
|
return validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected final <R extends Comparable<R>> DefaultValidatorOfComparable<T, R> ruleForComparable(Function<T, R> getter) {
|
||||||
|
DefaultValidatorOfComparable<T, R> validator = new DefaultValidatorOfComparable<>(getter);
|
||||||
|
this.rules.add(validator::validate);
|
||||||
|
return validator;
|
||||||
|
}
|
||||||
|
|
||||||
protected final IntValidator<T> ruleForInt(Function<T, Integer> getter) {
|
protected final IntValidator<T> ruleForInt(Function<T, Integer> getter) {
|
||||||
IntValidator<T> validator = new IntValidator<>(getter);
|
IntValidator<T> validator = new IntValidator<>(getter);
|
||||||
this.rules.add(validator::validate);
|
this.rules.add(validator::validate);
|
||||||
|
@ -0,0 +1,18 @@
|
|||||||
|
package xyz.zhouxy.plusone.validator;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
public class DefaultValidatorOfComparable<
|
||||||
|
TObj,
|
||||||
|
TProperty extends Comparable<TProperty>
|
||||||
|
> extends ValidatorOfComparable<TObj, TProperty, DefaultValidatorOfComparable<TObj, TProperty>> {
|
||||||
|
|
||||||
|
DefaultValidatorOfComparable(Function<TObj, ? extends TProperty> getter) {
|
||||||
|
super(getter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected DefaultValidatorOfComparable<TObj, TProperty> thisObject() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,7 @@ package xyz.zhouxy.plusone.validator;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class DoubleValidator<DTO> extends BasePropertyValidator<DTO, Double, DoubleValidator<DTO>> {
|
public class DoubleValidator<DTO> extends ValidatorOfComparable<DTO, Double, DoubleValidator<DTO>> {
|
||||||
|
|
||||||
DoubleValidator(Function<DTO, Double> getter) {
|
DoubleValidator(Function<DTO, Double> getter) {
|
||||||
super(getter);
|
super(getter);
|
||||||
|
@ -3,7 +3,7 @@ package xyz.zhouxy.plusone.validator;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class IntValidator<DTO> extends BasePropertyValidator<DTO, Integer, IntValidator<DTO>> {
|
public class IntValidator<DTO> extends ValidatorOfComparable<DTO, Integer, IntValidator<DTO>> {
|
||||||
|
|
||||||
IntValidator(Function<DTO, Integer> getter) {
|
IntValidator(Function<DTO, Integer> getter) {
|
||||||
super(getter);
|
super(getter);
|
||||||
|
@ -3,7 +3,7 @@ package xyz.zhouxy.plusone.validator;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class LongValidator<DTO> extends BasePropertyValidator<DTO, Long, LongValidator<DTO>> {
|
public class LongValidator<DTO> extends ValidatorOfComparable<DTO, Long, LongValidator<DTO>> {
|
||||||
|
|
||||||
LongValidator(Function<DTO, Long> getter) {
|
LongValidator(Function<DTO, Long> getter) {
|
||||||
super(getter);
|
super(getter);
|
||||||
|
@ -20,7 +20,7 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
|
|||||||
*
|
*
|
||||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
* @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 ValidatorOfComparable<DTO, String, StringValidator<DTO>> {
|
||||||
|
|
||||||
StringValidator(Function<DTO, String> getter) {
|
StringValidator(Function<DTO, String> getter) {
|
||||||
super(getter);
|
super(getter);
|
||||||
|
@ -0,0 +1,49 @@
|
|||||||
|
package xyz.zhouxy.plusone.validator;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
import com.google.common.collect.Range;
|
||||||
|
|
||||||
|
public abstract class ValidatorOfComparable<
|
||||||
|
TObj,
|
||||||
|
TProperty extends Comparable<TProperty>,
|
||||||
|
TPropertyValidator extends ValidatorOfComparable<TObj, TProperty, TPropertyValidator>
|
||||||
|
> extends BasePropertyValidator<TObj, TProperty, TPropertyValidator> {
|
||||||
|
|
||||||
|
ValidatorOfComparable(Function<TObj, ? extends TProperty> getter) {
|
||||||
|
super(getter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TPropertyValidator between(Range<TProperty> range) {
|
||||||
|
withRule(range::contains, convertExceptionCreator("The value is not in " + range.toString()));
|
||||||
|
return thisObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TPropertyValidator between(Range<TProperty> range, String errMsg) {
|
||||||
|
withRule(range::contains, convertExceptionCreator(errMsg));
|
||||||
|
return thisObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> TPropertyValidator between(
|
||||||
|
Range<TProperty> range,
|
||||||
|
Supplier<E> exceptionCreator) {
|
||||||
|
withRule(range::contains, exceptionCreator);
|
||||||
|
return thisObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> TPropertyValidator between(
|
||||||
|
Range<TProperty> range,
|
||||||
|
Function<TProperty, E> exceptionCreator) {
|
||||||
|
withRule(range::contains, exceptionCreator);
|
||||||
|
return thisObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
static <V> Function<V, IllegalArgumentException> convertExceptionCreator(String errMsg) {
|
||||||
|
return value -> new IllegalArgumentException(errMsg);
|
||||||
|
}
|
||||||
|
|
||||||
|
static <V, E extends RuntimeException> Function<V, E> convertExceptionCreator(Supplier<E> exceptionSupplier) {
|
||||||
|
return value -> exceptionSupplier.get();
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package xyz.zhouxy.plusone.validator.test;
|
package xyz.zhouxy.plusone.validator.test;
|
||||||
|
|
||||||
|
import java.time.Year;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -7,6 +8,8 @@ import java.util.Objects;
|
|||||||
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.collect.Range;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
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;
|
||||||
@ -19,7 +22,8 @@ class BaseValidatorTest {
|
|||||||
RegisterCommand registerCommand = new RegisterCommand("zhouxy108", "luquanlion@outlook.com", "22336",
|
RegisterCommand registerCommand = new RegisterCommand("zhouxy108", "luquanlion@outlook.com", "22336",
|
||||||
"A1b2C3d4",
|
"A1b2C3d4",
|
||||||
"A1b2C3d4",
|
"A1b2C3d4",
|
||||||
Arrays.asList(new String[] { "admin", "editor" }));
|
Arrays.asList(new String[] { "admin", "editor" }),
|
||||||
|
2000);
|
||||||
RegisterCommandValidator.INSTANCE.validate(registerCommand);
|
RegisterCommandValidator.INSTANCE.validate(registerCommand);
|
||||||
ValidTools.validate(registerCommand, RegisterCommandValidator.INSTANCE);
|
ValidTools.validate(registerCommand, RegisterCommandValidator.INSTANCE);
|
||||||
System.out.println(registerCommand);
|
System.out.println(registerCommand);
|
||||||
@ -30,6 +34,8 @@ class BaseValidatorTest {
|
|||||||
static final RegisterCommandValidator INSTANCE = new RegisterCommandValidator();
|
static final RegisterCommandValidator INSTANCE = new RegisterCommandValidator();
|
||||||
|
|
||||||
private RegisterCommandValidator() {
|
private RegisterCommandValidator() {
|
||||||
|
int thisYear = Year.now().getValue();
|
||||||
|
|
||||||
ruleForString(RegisterCommand::getUsername)
|
ruleForString(RegisterCommand::getUsername)
|
||||||
.isTrue(PredicateTools.<String>from(Objects::nonNull)
|
.isTrue(PredicateTools.<String>from(Objects::nonNull)
|
||||||
.and(StringUtils::isNotEmpty)
|
.and(StringUtils::isNotEmpty)
|
||||||
@ -48,6 +54,14 @@ class BaseValidatorTest {
|
|||||||
ruleForCollection(RegisterCommand::getRoles)
|
ruleForCollection(RegisterCommand::getRoles)
|
||||||
.notEmpty(() -> new RuntimeException("角色列表不能为空"));
|
.notEmpty(() -> new RuntimeException("角色列表不能为空"));
|
||||||
|
|
||||||
|
ruleForComparable(RegisterCommand::getYearOfBirth)
|
||||||
|
.notNull()
|
||||||
|
.between(Range.closed(thisYear - 60, thisYear - 18));
|
||||||
|
|
||||||
|
ruleForInt(RegisterCommand::getYearOfBirth)
|
||||||
|
.notNull()
|
||||||
|
.between(Range.closed(thisYear - 60, thisYear - 18));
|
||||||
|
|
||||||
withRule(registerCommand -> Objects.equals(registerCommand.getPassword(), registerCommand.getPassword2()),
|
withRule(registerCommand -> Objects.equals(registerCommand.getPassword(), registerCommand.getPassword2()),
|
||||||
"两次输入的密码不一致");
|
"两次输入的密码不一致");
|
||||||
}
|
}
|
||||||
@ -65,17 +79,20 @@ class BaseValidatorTest {
|
|||||||
private String password2;
|
private String password2;
|
||||||
private List<String> roles;
|
private List<String> roles;
|
||||||
|
|
||||||
|
private Integer yearOfBirth;
|
||||||
|
|
||||||
public RegisterCommand() {
|
public RegisterCommand() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public RegisterCommand(String username, String account, String code, String password, String password2,
|
public RegisterCommand(String username, String account, String code, String password, String password2,
|
||||||
List<String> roles) {
|
List<String> roles, Integer yearOfBirth) {
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.account = account;
|
this.account = account;
|
||||||
this.code = code;
|
this.code = code;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.password2 = password2;
|
this.password2 = password2;
|
||||||
this.roles = roles;
|
this.roles = roles;
|
||||||
|
this.yearOfBirth = yearOfBirth;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUsername() {
|
public String getUsername() {
|
||||||
@ -126,6 +143,14 @@ class BaseValidatorTest {
|
|||||||
this.roles = roles;
|
this.roles = roles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Integer getYearOfBirth() {
|
||||||
|
return yearOfBirth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setYearOfBirth(Integer yearOfBirth) {
|
||||||
|
this.yearOfBirth = yearOfBirth;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return new StringBuilder()
|
return new StringBuilder()
|
||||||
@ -136,6 +161,7 @@ class BaseValidatorTest {
|
|||||||
.append(", password=").append(password)
|
.append(", password=").append(password)
|
||||||
.append(", password2=").append(password2)
|
.append(", password2=").append(password2)
|
||||||
.append(", roles=").append(roles)
|
.append(", roles=").append(roles)
|
||||||
|
.append(", yearOfBirth=").append(yearOfBirth)
|
||||||
.append("]")
|
.append("]")
|
||||||
.toString();
|
.toString();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user