here we are with the stupid question of (more or less) 19h30.
I’m trying to learn Java using Spring Boot v.3 and to manage a FAKE database with it. This afternoon I was trying to implement some “constraints” for the username and password input via @/Pattern and @/Valid annotations.
That’s what I did in my User class:
@Column(nullable = false)
@Size(min = 5, max = 15)
@Pattern(regexp = "^(?=.*[A-Z])(?=.*\d).+$", message = "Password must contain at least one uppercase letter and one number")
private String password;
And what I did in my Controller class:
@PostMapping("/register")
public ResponseEntity<Object> registerUser(
u/Valid @RequestBody RegisterRequest registerRequest,
BindingResult result) {
try {
return ResponseEntity.ok(service.registerUser(registerRequest, result));
} ETC ETC
}
}
Since my Controller class is actually using a RegisterRequest DTO, to be sure I have also implemented the annotations there, like this:
The thing is that it is still accepting passwords with only two characters and without numbers of capitals. I’m sure it’s my fault, cannot be otherwise, but what can I do?
Please remember I’m only trying to learn and I’m dumb. Don’t be harsh.
I’ve thought that maybe I should “break” the RegisterRequest object in different strings when I send them to the service class in order to “validate” them before starting the service process.
And I did it, but…
…I’m not able to make it work. I mean, I was able to “break” the RegisterRequest Object in several strings, I sent them to UserService (that I have modified in order for it to receive three strings and a boolean) and if I try it with Postman the registration process still works, so the code is “good”. But the Valid annotation doesn’t work, it doesn’t check the constraints Size and Pattern.