asmvalidator

javabean validator based on asm

License

License

Categories

Categories

ASM Application Layer Libs Bytecode Manipulation
GroupId

GroupId

com.github.bingoohuang
ArtifactId

ArtifactId

asmvalidator
Last Version

Last Version

0.0.16
Release Date

Release Date

Type

Type

jar
Description

Description

asmvalidator
javabean validator based on asm
Project URL

Project URL

http://github.com/bingoohuang/asmvalidator
Source Code Management

Source Code Management

http://github.com/bingoohuang/asmvalidator

Download asmvalidator

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.bingoohuang/asmvalidator/ -->
<dependency>
    <groupId>com.github.bingoohuang</groupId>
    <artifactId>asmvalidator</artifactId>
    <version>0.0.16</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.bingoohuang/asmvalidator/
implementation 'com.github.bingoohuang:asmvalidator:0.0.16'
// https://jarcasting.com/artifacts/com.github.bingoohuang/asmvalidator/
implementation ("com.github.bingoohuang:asmvalidator:0.0.16")
'com.github.bingoohuang:asmvalidator:jar:0.0.16'
<dependency org="com.github.bingoohuang" name="asmvalidator" rev="0.0.16">
  <artifact name="asmvalidator" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.bingoohuang', module='asmvalidator', version='0.0.16')
)
libraryDependencies += "com.github.bingoohuang" % "asmvalidator" % "0.0.16"
[com.github.bingoohuang/asmvalidator "0.0.16"]

Dependencies

compile (5)

Group / Artifact Type Version
org.ow2.asm : asm jar 5.0.4
org.objenesis : objenesis jar 2.1
org.apache.commons : commons-lang3 jar 3.4
com.google.guava : guava jar 18.0
com.github.bingoohuang : java-utils jar 0.0.6

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok Optional jar 1.16.20

test (8)

Group / Artifact Type Version
junit : junit jar 4.12
com.google.truth : truth jar 0.40
org.springframework.boot : spring-boot-starter-web jar 1.2.5.RELEASE
com.jayway.jsonpath : json-path jar 1.2.0
org.springframework.boot : spring-boot-starter-test jar 1.2.5.RELEASE
com.fasterxml.jackson.core : jackson-databind jar 2.6.0
javax.servlet : javax.servlet-api jar 3.0.1
com.jayway.restassured : rest-assured jar 2.5.0

Project Modules

There are no modules declared in this project.

Build Status Coverage Status Maven Central License

asmvalidator

java bean validator based on asm.

Examples for JAVABEAN.

public class Person {
    String name;
    String addr;
    
    @AsmPast Date date;
    @AsmPast(format = "yyyy-MM-dd")
    String date2;
    
    @AsmFuture Date date;
    @AsmFuture(format = "yyyy-MM-dd")
    String date;
         
    @AsmIgnore String code;
    
    @AsmMaxSize(20) @AsmMessage("节目名称不能为空,长度不能超过20")
    String playName;
    
    @AsmMaxSize(7864320) @AsmMessage("图片不能超过5M")
    String portrait;

    @AsmSize(2) List<String> addresses;

    @AsmBlankable @AsmSize(6) String province;
    
    @AsmEmail String email;
    @AsmMobile String mobile;
    @AsmMobileOrEmail String mobileOrEmail;

    @AsmNotBlank AtomicBoolean some;
    
    @AsmMinSize(3) @AsmMaxSize(10) @AsmSize(4) int age;
    @AsmBlankable @AsmRegex("^\\w+$") String addr;
    
    @AsmRange("[10,100]") int age;
    @AsmRange("[A00,B99)") String addr;
    @AsmRange("男,女") String sex;
    @AsmRange("1,5,10,20,50,100") int rmb;
    @AsmRange("[10,]") int ageMin;
    @AsmRange("[,10]") int ageMax;
    @AsmRange("[A00,]") String code;
    @AsmRange("[A0,") String upperBound;
    
    @AsmUUID String uuid;
    
    @AsmBase64 String base64;
    @AsmBase64(purified = true) String other;
    @AsmBase64(format = UrlSafe) String third;
    
    @AsmValid
    public void customValidate() {
        // do some custom validations
        // throw runtime excption when failed.
    }

    @AsmValid
    public void customValidate(AsmValidateResult result) {
        // do some custom validations
        // add errors to result when failed.
    }
        
    @UrlsChecker
    List<String> urls;
    
    @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME)
    @AsmConstraint(supportedClasses = List.class, validateBy = UrlsChecker.AsmUrlsValidator.class)
    public @interface UrlsChecker {
        class AsmUrlsValidator implements MsaValidator<UrlsChecker, List<String>> {
            @Override public void validate(UrlsChecker annotation, AsmValidateResult result, List<String> urls) {
                for (int i = 0, ii = urls.size(); i < ii; ++i) {
                    val url = urls.get(i);
                    if (StringUtils.isEmpty(url)) {
                        result.addError(new ValidateError("urls_" + i, url, "URL不能为空"));
                    } else if (url.length() > 2) {
                        result.addError(new ValidateError("urls_" + i, url, "URL长度不能超过2"));
                    }
                }
    
            }
        }
    }
    
    @AsmBlankable @MsaSex private String sex;
    @AsmNotBlank @MsaSex(allowLadyboy = true) private String sex2;
    
    
    @AsmConstraint(validateBy = MsaSex.MsaSexValidator.class)
    @Target({FIELD, METHOD, PARAMETER, ANNOTATION_TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MsaSex {
        boolean allowLadyboy() default false;
    
        class MsaSexValidator implements MsaValidator<MsaSex, String> {
            @Override
            public void validate(MsaSex msaSex, AsmValidateResult result, String sex) {
                if ("".equals(sex) || "".equals(sex)) return;
                if (msaSex.allowLadyboy() && "人妖".equals(sex)) return;
    
                result.addError(new ValidateError("sex", sex, "性别非法"));
            }
    
        }
    }
    
    public static void main(String[] args) {
        Person person = new Person();
        // set person properties...
        
        // validate person bean
        AsmValidatorFactory.validateWithThrow(bean);
    }
}

Versions

Version
0.0.16
0.0.15
0.0.14
0.0.13
0.0.12
0.0.11
0.0.10
0.0.9
0.0.8
0.0.7
0.0.6
0.0.5
0.0.4
0.0.2
0.0.1