Skip to content

Commit

Permalink
feature: 添加验证@Length 注解
Browse files Browse the repository at this point in the history
  • Loading branch information
jamie1688 committed Sep 13, 2020
1 parent 19084e6 commit cc25b4b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.jamie.validation;

import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
Expand Down Expand Up @@ -32,6 +34,7 @@ public class UserInfo {
* NotBlank 自动去除字符串前后空格后验证是否为空
*/
@NotBlank(message = "用户密码不能为空")
@Length(min = 6, max = 20, message = "密码长度不能少于6位,大于20位")
private String passWord;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,40 +42,14 @@ public void init() {
userInfo2 = new UserInfo();
userInfo2.setUserId("雪花ID");
userInfo2.setUserName(" ");
userInfo2.setPassWord(" ");
userInfo2.setPassWord("11111");
}

/**
* 验证 @NotNull 注解
* 验证 validation 注解
*/
@Test
public void notNullValidation() {
// 使用验证器对对象进行验证
set1 = validator.validate(userInfo1);
set1.forEach(item -> System.out.println("验证1的结果" + item.getMessage()));
System.out.println("-------- 分隔符 --------");
set2 = validator.validate(userInfo2);
set2.forEach(item -> System.out.println("验证2的结果" + item.getMessage()));
}

/**
* 验证 @NotEmpty 注解
*/
@Test
public void notEmptyValidation() {
// 使用验证器对对象进行验证
set1 = validator.validate(userInfo1);
set1.forEach(item -> System.out.println("验证1的结果" + item.getMessage()));
System.out.println("-------- 分隔符 --------");
set2 = validator.validate(userInfo2);
set2.forEach(item -> System.out.println("验证2的结果" + item.getMessage()));
}

/**
* 验证 @NotBlank 注解
*/
@Test
public void notBlankValidation() {
public void validation() {
// 使用验证器对对象进行验证
set1 = validator.validate(userInfo1);
set1.forEach(item -> System.out.println("验证1的结果" + item.getMessage()));
Expand Down

0 comments on commit cc25b4b

Please sign in to comment.