Git address

Github.com/liangbaika/…

validate-springboot-starter

Maven Central Repository

        <dependency>
            <groupId>com.github.liangbaika</groupId>
            <artifactId>validate-springboot-starter</artifactId>
            <version>{latest}</version>
        </dependency>
Copy the code

Validate-springboot-starter (desc)

Validate-springboot-starter is a validate-Springboot-starter, a flexible and rich validation framework that seamlessly integrates with the Springboot framework.

versus springboot Framework for seamless integration of verification framework.

Note (attention)

1 If the illegal parameter will throw a ParamsValidException, you should catch this particular exception and resolve it. If you are using jSR303 (Javax-Validation) validation and do not use the @abcVALIDATE annotation, you need to handle the exception yourself. For example, ‘user.name’ supports a maximum of two levels of object authentication.

Express =validateBeanName when value is Custem. Some validation methods require multiple values separated by commas (,). Note that this Express field is tied to the value method of the validation anyway.

if illegal paramas then will throw ParamsValidException, and you should catch this special exception

and resolve it. Object multilevel authentication such as ‘user.name’ currently supports up to two levels.

Advantages

Integrated a lot of verification, such as mobile phone number verification, regular verification, email, numbers, decimals, license plate number, ID card, length, URL and other common verification. Compared to Javax Validation, our beans are reusable and more flexible, and can be completely decoupled from the business code as long as they are validation-related.

Integrated with a lot of authentication, such as phone number authentication, regular authentication, email, Numbers, decimals, license plate number, ID card, length, URL, and so on.Our beans are reusable and have more flexibility than Javax Validation.

(Custom) Custom

Just implement the ParamValidator interface, which handles complex validation and is completely decoupled from the business code (you need the Bean that implements the interface to be hosted by the Spring container); All you need to do is implement the ParamValidator interface, which handles complex validation and is completely decoupled from the business code (you need to have the Bean that implements the interface hosted by the Spring container).

Quick Start

package com.github.liangbaika.validate.test; import com.github.liangbaika.validate.annations.ValidateParam; import com.github.liangbaika.validate.annations.ValidateParams; import com.github.liangbaika.validate.enums.Check; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; /** * @author lq * @version 1.0 * @date 2020/6/16 11:56 */ @restController @requestMapping ("/test") public class TestController {/** * single field simple validation ** @param name * @return
     */
    @GetMapping("test0")
    @ValidateParam(value = Check.Length, argName = "name", express = "2, 6",msg="Custom prompt message")
    public Object test0(String name) {
        returnBoolean.TRUE; } /** * multi-field validation ** @param name * @param age * @param idcard * @return
     */
    @ValidateParams(
            value = {
                    @ValidateParam(value = Check.NotEmpty, argName = "name"),
                    @ValidateParam(value = Check.Number, argName = "age"),
                    @ValidateParam(value = Check.isIDCard, argName = "idcard"),
            }
    )
    @GetMapping("test1")
    public Object test1(String name, Integer age, Integer idcard) {
        returnBoolean.TRUE; } /** * Entity validation javax-validation method ** @param oneData * @return
     */
    @PostMapping("test2")
    public Object test2(@RequestBody @Valid OneData oneData) {
        returnoneData; } /** * object multi-level validation, mixed validation ** @param oneData * @return
     */
    @PostMapping("test3")
    @ValidateParams(
            value = {
                    @ValidateParam(value = Check.NotEmpty, argName = "oneData.name"),
                    @ValidateParam(value = Check.Number, argName = "oneData.age"),
            }
    )
    public Object test3(@RequestBody @Valid OneData oneData) {
        returnoneData; } /** * custom validation ** @param oneData * @return
     */
    @PostMapping("test4")
    @ValidateParam(value = Check.Custom, argName = "oneData.name", express = "nameValidater")
    public Object test4(@RequestBody OneData oneData) {
        return oneData;
    }

}


package com.github.liangbaika.validate.test;

import com.github.liangbaika.validate.annations.AbcValidate;
import com.github.liangbaika.validate.enums.Check;

/**
 * @author lq
 * @version 1.0
 * @date 2020/6/16 11:59
 */

public class OneData {


    @AbcValidate(required = true, fun = Check.Custom, express = "nameValidater")
    private String name;
    @AbcValidate(required = true)
    private Integer age;
    @AbcValidate(required = true, fun = Check.le, express = "1")
    private Integer sex;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) { this.sex = sex; } } package com.github.liangbaika.validate.test; import com.github.liangbaika.validate.core.ParamValidator; import org.springframework.stereotype.Component; /** * @author lq * @version 1.0 * @date 2020/6/16 12:01 */ @Component public class NameValidater implements ParamValidator { @Override public Boolean validate(Object value) { String name = (String) value;if (name.startsWith("Zhang") && name.length() == 3) {
            return true;
        }
        return false; }}Copy the code

Resolve error

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler({com.github.liangbaika.validate.exception.ParamsValidException .class})
    @ResponseBody
    public Results handleParamsValidException(com.github.liangbaika.validate.exception.ParamsValidException  e) {
        return Results.error(new ErrorCode(ErrorCode.PARAMA_ERROR.getCode(), "Parameter error"+ e.getMessage())); }}Copy the code

Thanks and Suggestions

This project uses the Apache License. You are welcome to use it or make suggestions or contribute code. If PR is required, please follow the standard PR principles. You are welcome to use or suggest or contribute code If PR is required, please follow the set of standard principles mentioned in the standard.