数据验证
背景:
在写框架时,有时不会有到实体Bean,这时数据验证有的头大,故产生了该工具 。本文原创
代码:
1 import java.text.ParseException; 2 import java.text.SimpleDateFormat; 3 import java.time.format.DateTimeFormatter; 4 import java.util.LinkedHashMap; 5 import java.util.Map; 6 import java.util.function.BiFunction; 7 import java.util.regex.Pattern; 8 9 public class Validt { 10 public final static String USERNAME ="userName"; 11 public final static String NUM ="num"; 12 public final static String NOTBLANK ="notBlank"; 13 public final static String REQUIRED ="required"; 14 public final static String MAX_LENGTH ="maxLength"; 15 public final static String EMAIL ="email"; //电邮 16 public final static String IDNUM ="idNum"; //身份证 17 public final static String DATE ="date"; //日期 18 public final static String URL ="url"; //URL 19 public final static String CHINESE ="chines"; //汉字 20 public final static String TEL ="tel"; //汉字 21 22 private String code; 23 private String rule; 24 25 private static Map<String, BiFunction<String,Object[],Boolean>> ruleMap; 26 private static Map<String, String> sysRuleMap=new LinkedHashMap<>(); 27 static { 28 sysRuleMap.put(USERNAME,"^[a-zA-Z]\\w{5,20}$"); 29 sysRuleMap.put(NUM,"^[-\\+]?\\d+(\\.\\d+)?$"); 30 sysRuleMap.put(NOTBLANK,""); 31 sysRuleMap.put(REQUIRED,""); 32 sysRuleMap.put(MAX_LENGTH,""); 33 sysRuleMap.put(EMAIL,"^\\w+([_-+.]\\w+)*@\\w+([_+-.]\\w+)*\\.\\w+([-.]\\w+)*$"); 34 sysRuleMap.put(IDNUM,"(^\\d{18}$)|(^\\d{15}$)"); 35 sysRuleMap.put(DATE,""); 36 sysRuleMap.put(URL,"http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?"); 37 sysRuleMap.put(CHINESE,"[\u4e00-\u9fa5],{0,}$"); 38 sysRuleMap.put(TEL,"^((\\(\\d{2,3}\\))|(\\d{3}\\-))?(\\(0\\d{2,3}\\)|0\\d{2,3}-)?[1-9]\\d{6,7}(\\-\\d{1,4})?"); 39 sysRuleMap.put(DATE,""); 40 41 } 42 43 public Validt( ) { } 44 45 46 public static boolean valid(String ruleCode, String content, Object ... parms){ 47 48 if (ruleMap!=null && ruleMap.containsKey(ruleCode) ){ 49 BiFunction<String,Object[],Boolean> ruleFun=ruleMap.get(ruleCode); 50 51 return ruleFun.apply(content,parms); 52 53 } 54 String rulestr = sysRuleMap.get(ruleCode); 55 if (rulestr==null ) { 56 return true; 57 } else if ( rulestr!=null && content==null ) { 58 return false; 59 } 60 61 62 if (ruleCode.equals(NUM)) { 63 if (parms.length>0 ){ 64 String d = parms[0].toString(); 65 rulestr = "^[-\\+]?\\d+.[\\d]{"+d+","+d+"}$"; 66 } 67 }else if (ruleCode.equals(DATE)) { 68 if (parms.length>0 ){ 69 String d = parms[0].toString(); 70 return isDate(content,d,false); 71 72 } else{ 73 return isDate(content); 74 } 75 }else if (ruleCode.equals(NOTBLANK) || ruleCode.equals(REQUIRED)) { 76 if(content==null || content.trim().equals("")){ 77 return false; 78 } else { 79 return true; 80 } 81 }else if (ruleCode.equals(MAX_LENGTH)) { 82 if(content==null ){ 83 return false; 84 } 85 int len=0; 86 if (parms.length>0 ){ 87 String d = parms[0].toString(); 88 try{ 89 len =Integer.parseInt(d); 90 } catch (Exception e){ 91 return false; 92 } 93 } 94 return content.length()<=len ?true:false; 95 } 96 97 boolean bl = Pattern.matches(rulestr,content); 98 if (bl==false) return false; 99 return true; 100 } 101 102 /** 103 * 宽松判断,有点耗时 104 * @param dateStr 105 * @return 106 */ 107 public static boolean isDate(String dateStr ){ 108 boolean tmpBL= isDate(dateStr,"yyyy/MM/dd",false); 109 String[] fStrs = { 110 "yyyy/MM/dd","yyyy-MM-dd", 111 "yyyy-M-d","yyyy/M/d", 112 "yy/M/d","yy-M-d" 113 }; 114 for(String str:fStrs){ 115 tmpBL= isDate(dateStr,str,false); 116 if (tmpBL) return true; 117 } 118 119 120 return false; 121 } 122 123 public static boolean isDate(String dateStr,String formatStr,Boolean isLenient){ 124 SimpleDateFormat format = new SimpleDateFormat(formatStr); 125 try { 126 format.setLenient(isLenient); 127 128 format.parse(dateStr); 129 } catch (Exception e) { 130 return false; 131 } 132 return true; 133 } 134 public static boolean matches(String rulestr,String content){ 135 return Pattern.matches(rulestr,content); 136 } 137 /** 138 * 139 * @param code 140 * @param ruleFun 输入是要验证的内容,及参数,返回正则调试判断 141 */ 142 public static void addRule(String code,BiFunction<String,Object[],Boolean> ruleFun){ 143 if (ruleMap==null){ 144 ruleMap=new LinkedHashMap<>(); 145 } 146 ruleMap.put(code,ruleFun); 147 } 148 149 public static void remove(String code){ 150 if (ruleMap==null) return; 151 ruleMap.remove(code); 152 } 153 154 public Map<String, String> getSysRuleMap() { 155 return sysRuleMap; 156 } 157 158 public void setSysRuleMap(Map<String, String> sysRuleMap) { 159 this.sysRuleMap = sysRuleMap; 160 } 161 }
测试与使用
1 @Test 2 public void validTest(){ 3 boolean temp = Validt.valid(Validt.NOTBLANK,"-123"); 4 boolean temp1 = Validt.valid(Validt.NOTBLANK,""); 5 boolean temp2 = Validt.valid(Validt.NOTBLANK,null); 6 7 boolean tmp = Validt.valid(Validt.NUM,"-123"); 8 boolean tmp1 = Validt.valid(Validt.NUM,"123",2); 9 boolean tmp2 = Validt.valid(Validt.NUM,"123.22",2); 10 Object[] params ={3}; 11 boolean tmp3 = Validt.valid(Validt.NUM,"123.223",params ); 12 13 //注意,由于本验证工具不是线程安全的, 14 // 自定义规则时请在初始化进行,运行中不能修改任何值,只能调用方法。 15 Validt.addRule("myrule",(content,parms)->{ 16 return Validt.matches("^[\\w]+",content); 17 }); 18 boolean tmp4 = Validt.valid("myrule","123.223" ); 19 boolean tmp5 = Validt.valid("myrule","aab" ); 20 21 boolean tmp6 = Validt.valid(Validt.DATE,"2012-1-1" ); 22 boolean tmp7 = Validt.valid(Validt.DATE,"2012-11-11" ); 23 boolean tmp8 = Validt.valid(Validt.DATE,"2012-11-11 23:35" ); 24 boolean tmp9 = Validt.valid(Validt.DATE,"2012-11-11 25:35" ); 25 boolean tmp10 = Validt.valid(Validt.DATE,"2012-11-11" ,"yyyy/MM/dd" ); 26 boolean tmp11 = Validt.valid(Validt.DATE,"2012-11-11" ,"yyyy-MM-dd" ); 27 boolean tmp12 = Validt.valid(Validt.DATE,"2012-11-11 25:55:00" ,"yyyy-MM-dd HH:mm:ss" ); 28 boolean tmp13 = Validt.valid(Validt.DATE,"2012-11-11 23:55:00" ,"yyyy-MM-dd HH:mm:ss" ); 29 System.out.println(); }