Compare commits
2 Commits
c1a7bafaf6
...
49f2c437f5
Author | SHA1 | Date |
---|---|---|
ZhouXY108 | 49f2c437f5 | |
ZhouXY108 | a71d75029e |
|
@ -21,18 +21,37 @@ import xyz.zhouxy.plusone.validator.map.MapValidator;
|
|||
public //
|
||||
class MapValidatorTests {
|
||||
|
||||
private static final String USERNAME = "username";
|
||||
private static final String ACCOUNT = "account";
|
||||
private static final String PASSWORD = "password";
|
||||
private static final String PASSWORD2 = "password2";
|
||||
private static final String AGE = "age";
|
||||
private static final String BOOLEAN = "boolean";
|
||||
private static final String ROLE_LIST = "roleList";
|
||||
private static final MapValidator<String, Object> validator = ParamsValidator.INSTANCE;
|
||||
|
||||
private static final MapValidator<String, Object> validator = new MapValidator<String, Object>(
|
||||
new String[] { USERNAME, ACCOUNT, PASSWORD, AGE, BOOLEAN, ROLE_LIST }) {
|
||||
{
|
||||
@Test
|
||||
void testValidateAndCopy() {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put(ParamsValidator.USERNAME, "ZhouXY");
|
||||
params.put(ParamsValidator.ACCOUNT, "zhouxy@code108.cn");
|
||||
params.put(ParamsValidator.PASSWORD, "99Code108");
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
class ParamsValidator extends MapValidator<String, Object> {
|
||||
public static final String USERNAME = "username";
|
||||
public static final String ACCOUNT = "account";
|
||||
public static final String PASSWORD = "password";
|
||||
public static final String PASSWORD2 = "password2";
|
||||
public static final String AGE = "age";
|
||||
public static final String BOOLEAN = "boolean";
|
||||
public static final String ROLE_LIST = "roleList";
|
||||
|
||||
public static final ParamsValidator INSTANCE = new ParamsValidator();
|
||||
|
||||
private ParamsValidator() {
|
||||
super(new String[] { USERNAME, ACCOUNT, PASSWORD, AGE, BOOLEAN, ROLE_LIST });
|
||||
checkValue(USERNAME, String.class).withRule(
|
||||
PredicateTools.from(StringUtils::isNotBlank)
|
||||
.and(username -> RegexTools.matches(username, PatternConsts.USERNAME)),
|
||||
|
@ -65,22 +84,5 @@ class MapValidatorTests {
|
|||
.withRule(CollectionTools::isNotEmpty, "角色列表不能为空!")
|
||||
.withRule(l -> l.stream().allMatch(StringUtils::isNotBlank),
|
||||
() -> new IllegalArgumentException("角色标识不能为空!"));
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
@Test
|
||||
void testValidateAndCopy() {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("username", "ZhouXY");
|
||||
params.put("account", "zhouxy@code108.cn");
|
||||
params.put("password", "99Code108");
|
||||
params.put("password2", "99Code108");
|
||||
params.put("age", 18);
|
||||
params.put("boolean", true);
|
||||
params.put("roleList", Arrays.asList("admin", ""));
|
||||
|
||||
Map<String, Object> validedParams = validator.validateAndCopy(params);
|
||||
System.out.println(validedParams);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue