項目參見:
https://gitee.com/xxssyyyyssxx/validation
compile "top.jfunc.validation:validation-core:1.0.1"
compile "top.jfunc.validation:validation-spring:1.0.1"
all input is evil!!
服務端參數校驗是保證數據一致性和避免髒數據的最後關口,必須保證參數符合要求才能通過,所以參數校驗及其重要。參數校驗說白了就是對輸入值進行判斷,如果符合預期,那麼就讓通過,否則直接拋出異常。基於此,在gitee上遇到了實現此想法的非常好的項目。https://gitee.com/fetech-framework/smart-validator 。在此項目基礎上做了一些優化形成自己的項目:https://gitee.com/xxssyyyyssxx/validation。優化的地方主要爲:
- 命名優化,比如ValidateUtils->ValidateValue,is->with使意義更明確
- 正則表達式使用Pattern緩存
- 去除ParamException異常,使用IllegalArgumentException替代
- 小bug修復。
使用方式爲:
ValidateValue.with(jsonObject.getString("name")).notNull("名字不允許爲空")
.and(jsonObject.getString("idCard")).notNull("身份證不允許爲空")
.and(jsonObject.getString("cardFront")).notNull("身份證正面照片不允許爲空")
.and(jsonObject.getString("cardBack")).notNull("身份證反面照片不允許爲空");