重构代码,MapValidator 可使用不同类型的 PropertyValidator。
parent
49f2c437f5
commit
3cac270574
|
@ -1,4 +1,4 @@
|
||||||
package xyz.zhouxy.plusone.validator.map;
|
package xyz.zhouxy.plusone.validator;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -26,6 +26,8 @@ public abstract class MapValidator<K, V> {
|
||||||
this.keys = keys.stream().collect(Collectors.toSet());
|
this.keys = keys.stream().collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ========== validate & validateAndCopy ==========
|
||||||
|
|
||||||
public final Map<K, V> validateAndCopy(Map<K, V> obj) {
|
public final Map<K, V> validateAndCopy(Map<K, V> obj) {
|
||||||
return validateAndCopyInternal(obj, this.keys);
|
return validateAndCopyInternal(obj, this.keys);
|
||||||
}
|
}
|
||||||
|
@ -50,17 +52,47 @@ public abstract class MapValidator<K, V> {
|
||||||
this.consumers.forEach(consumer -> consumer.accept(obj));
|
this.consumers.forEach(consumer -> consumer.accept(obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
// ========== ruleFor ==========
|
||||||
protected final <VV extends V> EntryValidator<K, VV> checkValue(K key, Class<VV> clazz) {
|
|
||||||
return checkValue(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected final <VV extends V> EntryValidator<K, VV> checkValue(K key) {
|
protected final ObjectValidator<Map<K, V>, V> ruleFor(K key) {
|
||||||
EntryValidator<K, VV> validator = new EntryValidator<>(key);
|
ObjectValidator<Map<K, V>, V> validator = new ObjectValidator<>(m -> m.get(key));
|
||||||
this.consumers.add(validator::validate);
|
this.consumers.add(validator::validate);
|
||||||
return validator;
|
return validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected final IntValidator<Map<K, V>> ruleForInt(K key) {
|
||||||
|
IntValidator<Map<K, V>> validator = new IntValidator<>(m -> (Integer) m.get(key));
|
||||||
|
this.consumers.add(validator::validate);
|
||||||
|
return validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final DoubleValidator<Map<K, V>> ruleForDouble(K key) {
|
||||||
|
DoubleValidator<Map<K, V>> validator = new DoubleValidator<>(m -> (Double) m.get(key));
|
||||||
|
this.consumers.add(validator::validate);
|
||||||
|
return validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final BoolValidator<Map<K, V>> ruleForBool(K key) {
|
||||||
|
BoolValidator<Map<K, V>> validator = new BoolValidator<>(m -> (Boolean) m.get(key));
|
||||||
|
this.consumers.add(validator::validate);
|
||||||
|
return validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final StringValidator<Map<K, V>> ruleForString(K key) {
|
||||||
|
StringValidator<Map<K, V>> validator = new StringValidator<>(m -> (String) m.get(key));
|
||||||
|
this.consumers.add(validator::validate);
|
||||||
|
return validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final <E> CollectionValidator<Map<K, V>, E> ruleForCollection(K key) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
CollectionValidator<Map<K, V>, E> validator = new CollectionValidator<>(m -> (Collection<E>) m.get(key));
|
||||||
|
this.consumers.add(validator::validate);
|
||||||
|
return validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ========== withRule ==========
|
||||||
|
|
||||||
protected final void withRule(Predicate<? super Map<K, V>> rule, String errMsg) {
|
protected final void withRule(Predicate<? super Map<K, V>> rule, String errMsg) {
|
||||||
withRule(rule, map -> new IllegalArgumentException(errMsg));
|
withRule(rule, map -> new IllegalArgumentException(errMsg));
|
||||||
}
|
}
|
|
@ -1,22 +0,0 @@
|
||||||
package xyz.zhouxy.plusone.validator.map;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.validator.BasePropertyValidator;
|
|
||||||
|
|
||||||
public class EntryValidator<K, V>
|
|
||||||
extends BasePropertyValidator<Map<K, ? super V>, V, EntryValidator<K, V>> {
|
|
||||||
|
|
||||||
public EntryValidator(K key) {
|
|
||||||
super(m -> {
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
V v = (V) m.get(key);
|
|
||||||
return v;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected EntryValidator<K, V> thisObject() {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -3,7 +3,6 @@ package xyz.zhouxy.plusone.validator.map.test;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
@ -12,11 +11,8 @@ 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 xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
|
||||||
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.validator.MapValidator;
|
||||||
import xyz.zhouxy.plusone.commons.util.RegexTools;
|
|
||||||
import xyz.zhouxy.plusone.validator.map.MapValidator;
|
|
||||||
|
|
||||||
public //
|
public //
|
||||||
class MapValidatorTests {
|
class MapValidatorTests {
|
||||||
|
@ -52,36 +48,33 @@ class ParamsValidator extends MapValidator<String, Object> {
|
||||||
|
|
||||||
private ParamsValidator() {
|
private ParamsValidator() {
|
||||||
super(new String[] { USERNAME, ACCOUNT, PASSWORD, AGE, BOOLEAN, ROLE_LIST });
|
super(new String[] { USERNAME, ACCOUNT, PASSWORD, AGE, BOOLEAN, ROLE_LIST });
|
||||||
checkValue(USERNAME, String.class).withRule(
|
ruleForString(USERNAME)
|
||||||
PredicateTools.from(StringUtils::isNotBlank)
|
.notBlank("用户名不能为空")
|
||||||
.and(username -> RegexTools.matches(username, PatternConsts.USERNAME)),
|
.matches(PatternConsts.USERNAME,
|
||||||
username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username)));
|
username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username)));
|
||||||
|
|
||||||
checkValue(ACCOUNT, String.class).withRule(
|
ruleForString(ACCOUNT)
|
||||||
PredicateTools.from(StringUtils::isNotBlank)
|
.notBlank("账号不能为空")
|
||||||
.and(account -> RegexTools.matchesOne(account,
|
.matchesOne(new Pattern[] { PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE }, "请输入正确的邮箱地址或手机号");
|
||||||
new Pattern[] { PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE })),
|
|
||||||
"请输入正确的邮箱地址或手机号");
|
|
||||||
|
|
||||||
checkValue(PASSWORD, String.class)
|
ruleForString(PASSWORD)
|
||||||
.withRule(StringUtils::isNotEmpty, "密码不能为空")
|
.notEmpty("密码不能为空")
|
||||||
.withRule(pwd -> RegexTools.matches(pwd, PatternConsts.PASSWORD), "密码不符合规范");
|
.matches(PatternConsts.PASSWORD, "密码不符合规范");
|
||||||
|
|
||||||
// 校验到多个属性,只能针对 map 本身进行校验
|
// 校验到多个属性,只能针对 map 本身进行校验
|
||||||
withRule(m -> Objects.equals(m.get(PASSWORD), m.get(PASSWORD2)),
|
withRule(m -> Objects.equals(m.get(PASSWORD), m.get(PASSWORD2)),
|
||||||
"两次输入的密码不一样!");
|
"两次输入的密码不一样!");
|
||||||
|
|
||||||
// 通过泛型方式调用方法,指定数据类型
|
ruleForInt(AGE)
|
||||||
this.<Integer>checkValue(AGE)
|
|
||||||
.withRule(Objects::nonNull)
|
.withRule(Objects::nonNull)
|
||||||
.withRule(age -> (18 <= age && 60 >= age));
|
.between(18, 61);
|
||||||
|
|
||||||
checkValue(BOOLEAN, Boolean.class)
|
ruleForBool(BOOLEAN)
|
||||||
.withRule(Objects::nonNull, "Boolean property could not be null.")
|
.notNull("Boolean property could not be null.")
|
||||||
.withRule(b -> b, "Boolean property must be true.");
|
.isTrue("Boolean property must be true.");
|
||||||
|
|
||||||
this.<Collection<String>>checkValue(ROLE_LIST)
|
this.<String>ruleForCollection(ROLE_LIST)
|
||||||
.withRule(CollectionTools::isNotEmpty, "角色列表不能为空!")
|
.notEmpty("角色列表不能为空!")
|
||||||
.withRule(l -> l.stream().allMatch(StringUtils::isNotBlank),
|
.withRule(l -> l.stream().allMatch(StringUtils::isNotBlank),
|
||||||
() -> new IllegalArgumentException("角色标识不能为空!"));
|
() -> new IllegalArgumentException("角色标识不能为空!"));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue