GithubHelp home page GithubHelp logo

mtumilowicz / java11-jsr303-custom-validation Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 1.0 85 KB

Example of custom validation with JSR303.

Java 100.00%
bean-validation validation jsr303 jsr303-bean-validation jsr303-bean

java11-jsr303-custom-validation's Introduction

Build Status

java11-jsr303-custom-validation

preface

  • https://github.com/mtumilowicz/java-bean-validation2
  • JSR303 - https://beanvalidation.org/2.0/spec/
  • validating data is a common task that occurs throughout an application, from the presentation layer to the persistence layer
  • often the same validation logic is implemented in each layer, proving to be time consuming and error-prone
  • JSR defines a metadata model and API for JavaBean validation
  • default metadata source is annotations, with the ability to override and extend the metadata through the use of XML validation descriptors
  • declaring these constraints does not automatically cause their validation when the concerned methods are invoked - it’s the responsibility of an integration layer (Spring, Hibernate...) to trigger the validation of the constraints using a method interceptor, dynamic proxy or similar
  • bean Validation itself doesn’t trigger the evaluation of method constraints
  • just annotating any methods or constructors with parameter or return value constraints doesn’t automatically enforce these constraints, just as annotating any fields or properties with bean constraints doesn’t enforce these either
  • @Valid - tells Spring to pass the object to a Validator before doing anything else
  • if the input class contains a field with another complex type that should be validated, this field, needs to be annotated with @Valid

project description

  • we have User
    public class User {
        String name;
    }
    
    and we want to validate if a name is a proper word
  • regex for a word character ([a-zA-Z_0-9]): [\w]
  • JSR303
    • annotation
      @Constraint(validatedBy = WordValidator.class)
      @Target(ElementType.FIELD)
      @Retention(RetentionPolicy.RUNTIME)
      public @interface Word {
          String message() default "is not a proper word!";
          Class<?>[] groups() default {};
          Class<? extends Payload>[] payload() default {};
      }
      
      • we need groups() and payload(), otherwise Internal Server Error, 500 - "HV000074: com.example.validation.app.domain.patterns.Word contains Constraint annotation, but does not contain a groups parameter."
    • validator
      class WordValidator implements ConstraintValidator<Word, String> {
      
          private static final Predicate<String> PATTERN = Pattern.compile("[\\w]+").asMatchPredicate();
      
          @Override
          public void initialize(Word word) {
          }
      
          @Override
          public boolean isValid(String word,
                                 ConstraintValidatorContext cxt) {
              return isNull(word) || PATTERN.test(word);
          }
      
      }
      
  • endpoint
    @RestController
    @RequestMapping("/users")
    public class UserController {
        
        @PostMapping("register")
        public ResponseEntity<User> register(@RequestBody @Valid User user) {
            return ResponseEntity.ok(user);
        }
    }
    
  • final version of User
    @Value
    @Builder
    public class User {
    
        @NotBlank
        @Word
        String name;
    
        @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
        public User(@JsonProperty("name") String name) {
            this.name = name;
        }
    }
    
    • @JsonCreator and dedicated constructor are to prevent JSON parse error: cannot deserialize from Object value, note that User is immutable
  • swagger: http://localhost:8080/swagger-ui.html#
    • users/register, response for a user with %%% as name:
      {
        "timestamp": "2019-03-11T09:03:53.983+0000",
        "status": 400,
        "error": "Bad Request",
        "errors": [
          {
            "codes": [
              "Word.user.name",
              "Word.name",
              "Word.java.lang.String",
              "Word"
            ],
            "arguments": [
              {
                "codes": [
                  "user.name",
                  "name"
                ],
                "arguments": null,
                "defaultMessage": "name",
                "code": "name"
              }
            ],
            "defaultMessage": "is not a proper word!",
            "objectName": "user",
            "field": "name",
            "rejectedValue": "%%%",
            "bindingFailure": false,
            "code": "Word"
          }
        ],
        "message": "Validation failed for object='user'. Error count: 1",
        "path": "/users/register"
      }
      

java11-jsr303-custom-validation's People

Contributors

mtumilowicz avatar

Watchers

 avatar

Forkers

dineshbhagat

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.