mirror of https://github.com/halo-dev/halo
Fix #173
parent
246bfedf01
commit
7e85cd6887
|
@ -21,27 +21,27 @@ import javax.validation.constraints.Size;
|
||||||
@Data
|
@Data
|
||||||
public class UserParam implements InputConverter<User> {
|
public class UserParam implements InputConverter<User> {
|
||||||
|
|
||||||
@NotBlank(message = "用户名不能为空", groups = {AllCheck.class})
|
@NotBlank(message = "用户名不能为空", groups = {CreateCheck.class, UpdateCheck.class})
|
||||||
@Size(max = 50, message = "用户名的字符长度不能超过 {max}", groups = {AllCheck.class})
|
@Size(max = 50, message = "用户名的字符长度不能超过 {max}", groups = {CreateCheck.class, UpdateCheck.class})
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
@NotBlank(message = "用户昵称不能为空", groups = {AllCheck.class})
|
@NotBlank(message = "用户昵称不能为空", groups = {CreateCheck.class, UpdateCheck.class})
|
||||||
@Size(max = 255, message = "用户昵称的字符长度不能超过 {max}", groups = {AllCheck.class})
|
@Size(max = 255, message = "用户昵称的字符长度不能超过 {max}", groups = {CreateCheck.class, UpdateCheck.class})
|
||||||
private String nickname;
|
private String nickname;
|
||||||
|
|
||||||
@Email(message = "电子邮件地址的格式不正确", groups = {AllCheck.class})
|
@Email(message = "电子邮件地址的格式不正确", groups = {CreateCheck.class, UpdateCheck.class})
|
||||||
@NotBlank(message = "电子邮件地址不能为空", groups = {AllCheck.class})
|
@NotBlank(message = "电子邮件地址不能为空", groups = {CreateCheck.class, UpdateCheck.class})
|
||||||
@Size(max = 127, message = "电子邮件的字符长度不能超过 {max}", groups = {AllCheck.class})
|
@Size(max = 127, message = "电子邮件的字符长度不能超过 {max}", groups = {CreateCheck.class, UpdateCheck.class})
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
@Null(groups = UpdateCheck.class)
|
@Null(groups = UpdateCheck.class)
|
||||||
@Size(min = 8, max = 100, message = "密码的字符长度必须在 {min} - {max} 之间", groups = {CreateCheck.class})
|
@Size(min = 8, max = 100, message = "密码的字符长度必须在 {min} - {max} 之间", groups = {CreateCheck.class})
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
@Size(max = 1023, message = "头像链接地址的字符长度不能超过 {max}", groups = {AllCheck.class})
|
@Size(max = 1023, message = "头像链接地址的字符长度不能超过 {max}", groups = {CreateCheck.class, UpdateCheck.class})
|
||||||
private String avatar;
|
private String avatar;
|
||||||
|
|
||||||
@Size(max = 1023, message = "用户描述的字符长度不能超过 {max}", groups = {AllCheck.class})
|
@Size(max = 1023, message = "用户描述的字符长度不能超过 {max}", groups = {CreateCheck.class, UpdateCheck.class})
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
package run.halo.app.model.params;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.Test;
|
||||||
|
import run.halo.app.model.support.AllCheck;
|
||||||
|
import run.halo.app.model.support.CreateCheck;
|
||||||
|
|
||||||
|
import javax.validation.ConstraintViolation;
|
||||||
|
import javax.validation.Validation;
|
||||||
|
import javax.validation.Validator;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author johnniang
|
||||||
|
* @date 19-6-1
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class InstallParamTest {
|
||||||
|
|
||||||
|
private final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void createCheckTest() {
|
||||||
|
InstallParam installParam = new InstallParam();
|
||||||
|
|
||||||
|
Set<ConstraintViolation<InstallParam>> constraintViolations = validator.validate(installParam, CreateCheck.class);
|
||||||
|
assertThat(constraintViolations.size(), equalTo(4));
|
||||||
|
printMessage(constraintViolations);
|
||||||
|
|
||||||
|
installParam.setEmail("test");
|
||||||
|
constraintViolations = validator.validate(installParam, CreateCheck.class);
|
||||||
|
assertThat(constraintViolations.size(), equalTo(4));
|
||||||
|
printMessage(constraintViolations);
|
||||||
|
|
||||||
|
installParam.setEmail("test@test.com");
|
||||||
|
constraintViolations = validator.validate(installParam, CreateCheck.class);
|
||||||
|
assertThat(constraintViolations.size(), equalTo(3));
|
||||||
|
printMessage(constraintViolations);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void printMessage(Set<ConstraintViolation<InstallParam>> constraintViolations) {
|
||||||
|
if (constraintViolations == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug("");
|
||||||
|
|
||||||
|
constraintViolations.forEach(constraintViolation -> log.debug(constraintViolation.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue