Android Common Validator using AspectJ , Annotation , Reflect. Support custom rule and validator
基于AspectJ,注解,反射开发的Android方法、参数校验库,支持自定义规则校验
dependencies {
...
compile 'com.validator:validator-core:1.0.1@aar' // 1.0.1 support res warning
}
Android Application Module
apply from: 'https://raw.githubusercontent.com/aos3618/android-validator/master/appconfig.gradle'
Android Library Module
apply from: 'https://raw.githubusercontent.com/aos3618/android-validator/master/libconfig.gradle'
在某些方法调用时检查参数的合法性并进行提示 目前提供的注解有(持续加入中):
注解 | 说明 | 支持类型 |
---|---|---|
NotBlank | 不可为空 | String,Collection,以及有getText()/size()方法的任意类型 |
NotNull | 不可为Null | 任意类型 |
Pattern(regex) | 是否是指定的正则表达式 | String,以及有getText()方法的任意类型 |
Max(max) | 是否<=max | int |
Min(min) | 是否>=min | int |
Size(min,max) | 长度是否在min-max之间 | String,Collection,以及有getText()/size()方法的任意类型 |
Mobile | 是否是一个手机号码 | String,以及有getText()方法的任意类型 |
是否是一个邮箱 | String,以及有getText()方法的任意类型 |
public class DemoApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Validator.init(
ValidatorConfig
.newInstance()
.setApplication(this)
.setWarningProvider(new WarningProvider()) //custom how to show warning, default is Log
.setRuleProvider(new RuleProvider()) //custom the check rule
);
}
class WarningProvider implements IWarningProvider {
...
}
class RuleProvider implements IRuleProvider {
...
}
}
public class MainActivity extends AppCompatActivity {
//validate if mTV is a mobile when @Validate method invoked
@Mobile(warning = "wrong mobile number 1")
TextView mTV;
@Mobile(warning = "wrong mobile number 2")
String mText = "15022729132";
//validate if mEmail is a email when @Validate method invoked
@Email(warning = "wrong email 1")
String mEmail;
//validate if mEmailText is not empty when @Validate method invoked
@NotBlank(warning = "a empty field")
@CustomeValidator
TextView mEmailText;
//validate if mValue is less than 10 when @Validate method invoked
@Max(value = 10, warning = "need less than 10")
int mValue = 8;
//validate if mPattern matched the regex when @Validate method invoked
@Pattern(value = "^[0-9]*$", warning = "not matched regex")
String mPattern;
//validate if mList size is 0-2 when @Validate method invoked
//use warningId can show a res warning
@Size(min = 0, max = 2, warningId = R.string.waring)
List<String> mList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Validator.inject(this);
mEmailText = findViewById(R.id.tv_email);
mEmailText.setText("123");
mTV = findViewById(R.id.tv_text);
mTV.setText("13651234143");
mEmail = "@";
mPattern = "123";
findViewById(R.id.tv_list).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
validate3(mList);
}
});
mTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
validate1("11", 8);
}
});
mEmailText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
validate2("123");
}
});
}
//validate the field :mTV,mText;
//validate the parameter : s , i
@Validate({"mTV", "mText"})
public void validate1(@CustomeValidator String s,
@Min(value = 10, warning = "need more than 10") int i) {
}
//validate the field :mEmail,mEmailText;
//validate the parameter : pattern
@Validate({"mEmail", "mEmailText"})
public void validate2(@Pattern(value = "^[0-9]*$", warning = "not matched regex") String pattern) {
}
//validate the field :mPattern,mValue;
//validate the parameter : list
@Validate({"mPattern", "mValue"})
public void validate3(@Size(min = 1, max = 2, warning = "wrong list size") List list) {
}
}
@Documented
@Target({FIELD, PARAMETER})
@Retention(RUNTIME)
@Constraint(LoginRule.class) //Must have a Constraint Annotation,and define a check rule
public @interface LoginValiadtor {
String value() default "LoginValidator";
}
public class LoginRule implements IRuleValidator<LoginValiadtor> { //need implement IRuleValidator and step1 Annotation as generics
UserInfo userInfo;
@Override
public void initialize(LoginValiadtor loginValiadtor, Object t) {
if (t instanceof UserInfo) {
userInfo = (UserInfo) t;
}
}
@Override
public boolean isValid() {
return userInfo != null && userInfo.isLogin(); //check the login status
}
@Override
public void showWarning() {
// Toast.makeText(context, "请登录", Toast.LENGTH_SHORT);
Log.d("TAG", "请登录");
}
}
与默认注解一致
MIT License
Copyright (c) 2019 Liu zhanqi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.