增加Validator增加两个验证方法

This commit is contained in:
hellozrh 2022-08-09 15:20:37 +08:00
parent aa403cbe98
commit b8977ea119
2 changed files with 63 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.IdcardUtil; import cn.hutool.core.util.IdcardUtil;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.util.Date;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -1046,6 +1047,29 @@ public class Validator {
} }
} }
/**
* 检查给定的日期是否在指定范围内
*
* @param value
* @param start 最小值包含
* @param end 最大值包含
* @param errorMsg 验证错误的信息
* @throws ValidateException 验证异常
* @author ZRH 455741807@qq.com
*/
public static void validateBetween(final Date value, final Date start, final Date end, final String errorMsg) {
Assert.notNull(value);
Assert.notNull(start);
Assert.notNull(end);
int c1 = DateUtil.compare(value, start);
int c2 = DateUtil.compare(value, end);
if(c1 >= 0 && c2 <= 0){
return;
}
throw new ValidateException(errorMsg);
}
/** /**
* 是否是有效的统一社会信用代码 * 是否是有效的统一社会信用代码
* <pre> * <pre>
@ -1160,4 +1184,18 @@ public class Validator {
} }
return value; return value;
} }
/**
* 验证字符的长度是否符合要求
* @param str 字符串
* @param min 最小长度
* @param max 最大长度
* @param errorMsg 错误消息
*/
public static void validateLength(CharSequence str, int min, int max, String errorMsg) {
int len = StrUtil.length(str);
if (len < min || len > max) {
throw new ValidateException(errorMsg);
}
}
} }

View File

@ -1,11 +1,14 @@
package cn.hutool.core.lang; package cn.hutool.core.lang;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.exceptions.ValidateException; import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.core.lang.id.IdUtil; import cn.hutool.core.lang.id.IdUtil;
import cn.hutool.core.regex.PatternPool; import cn.hutool.core.regex.PatternPool;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.util.Date;
/** /**
* 验证器单元测试 * 验证器单元测试
* *
@ -248,6 +251,28 @@ public class ValidatorTest {
Assert.assertTrue(Validator.isMatchRegex(PatternPool.URL_HTTP, content)); Assert.assertTrue(Validator.isMatchRegex(PatternPool.URL_HTTP, content));
} }
@Test
public void validateBetweenTest(){
Date value = DateUtil.parse("2022-08-09 21:22:00");
final Date start = DateUtil.parse("2022-08-01 21:22:00");
final Date end = DateUtil.parse("2022-09-09 21:22:00");
Validator.validateBetween(value, start, end, "您选择的日期超出规定范围!");
Assert.assertTrue(true);
final Date value2 = DateUtil.parse("2023-08-09 21:22:00");
Assert.assertThrows(ValidateException.class, ()->
Validator.validateBetween(value2, start, end, "您选择的日期超出规定范围!")
);
}
@Test
public void validateLengthTest() {
String s1 = "abc";
Assert.assertThrows(ValidateException.class, ()->
Validator.validateLength(s1, 6, 8, "请输入6到8位的字符"));
}
@Test @Test
public void isChineseNameTest(){ public void isChineseNameTest(){
Assert.assertTrue(Validator.isChineseName("阿卜杜尼亚孜·毛力尼亚孜")); Assert.assertTrue(Validator.isChineseName("阿卜杜尼亚孜·毛力尼亚孜"));