Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. public class User {
  2. @NotEmpty
  3. private String email;
  4. @JsonProperty(access = Access.WRITE_ONLY)
  5. @NotEmpty
  6. private String password;
  7.  
  8. // Getters and setters
  9. ...
  10. }
  11.  
  12. @Path("/register")
  13. @POST
  14. public Response register(@Valid User user) {
  15. ...
  16. }
  17.  
  18. "{"errors":["password may not be empty"]}"
  19.  
  20. public class GroupValidationTest {
  21.  
  22.  
  23. public static void main(String[] args) {
  24. Validator v = Validators.newValidator();
  25.  
  26.  
  27. Model m = new Model();
  28. m.user = "Harry";
  29. m.password = "Potter";
  30.  
  31. Set<ConstraintViolation<Model>> validate = v.validate(m, INPUT.class);
  32. System.out.println(validate.size());
  33.  
  34. validate = v.validate(m, INPUT.class, OUTPUT.class);
  35. System.out.println(validate.size());
  36.  
  37. validate = v.validate(m, OUTPUT.class);
  38. System.out.println(validate.size());
  39.  
  40. m.password = null;
  41.  
  42. validate = v.validate(m, INPUT.class, OUTPUT.class);
  43. System.out.println(validate.size());
  44.  
  45. validate = v.validate(m, OUTPUT.class);
  46. System.out.println(validate.size());
  47. }
  48.  
  49. public static class Model {
  50.  
  51. @NotEmpty(groups={INPUT.class, OUTPUT.class})
  52. public String user;
  53.  
  54. @NotEmpty(groups={INPUT.class})
  55. public String password;
  56.  
  57. }
  58.  
  59. public interface INPUT {}
  60. public interface OUTPUT {}
  61.  
  62. }
  63.  
  64. 0 -> Full object, validate INPUT
  65. 0 -> Full object, validate INPUT + OUTPUT
  66. 0 -> Full object, validate OUTPUT
  67. 1 -> null password, validate INPUT + OUTPUT
  68. 0 -> null password, validate OUTPUT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement