Giter Site home page Giter Site logo

asmvalidator's Introduction

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);
    }
}

asmvalidator's People

Contributors

bingoohuang avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.