0

I have some DTO object with validation annotations:

MyDto { 
@NotBlank(message = "subscription name is empty")
@Size(max = Constants.SUBSCRIPTION_NAME_MAX_LENGTH, message = "size must not exceed 64 symbols")
String name;
}

Also I have wrapper class for the MyDto:

MyRequest {
    @Size(min = 1, max = 10, message = "My dto list must contain from 1 to 10 items")
    List<@Valid MyDto> dtos;
}

And I finally have controller:

@PostMapping("/doSomething")
 @ResponseStatus(HttpStatus.MULTI_STATUS)
 public Flux<Result> doSomething(@RequestBody @Valid MyRequest request) { //doing something }

                                            

Validation works fine but at some point I want to do manual validation of MyDto using provided validator. I expect if remove @Valid annotations from controller method and from MyRequest wrapper class it will not do automatic validation. However, when I remove all these @Valid annotations from those places and call controller, spring boot does the validation anyway.

Most likely I am missing something but don't know what...Will appreciate if somebody helps...

Aslan
  • 21
  • 5
  • If you remove `@Valid` it will not do the validation. At least not at the controller level, if you have something like JPA that might enable validation as well. So I doubt it is actually the controller doing the validation but something else down the line. – M. Deinum Jul 13 '23 at 07:20
  • It doesn't enter into controller method in debug mode. It happens before my code runs – Aslan Jul 13 '23 at 07:26
  • If there are no annotations on either the controller nor the object (as you state) there is no way there is validation going to happen. As there simply is nothing to validate. So there is something you aren't showing/telling/explaining in your setup that does validation as well. Or you simply didn't rebuild the classes so the old classes are still in use. I also wonder what error you get and what happens in your logging. – M. Deinum Jul 13 '23 at 07:28
  • Actually MyDto class has all @NotBlank and similar annotations there because I want still trigger validation manually. I am not sure that "non rebuild classes" is the case as I re-run app each time I make changes – Aslan Jul 13 '23 at 07:34
  • Your DTO doesn't matter here as it is the `MyRequest` from which you state you removed **all annotations** and you removed the `@Valid` from the controller method. So there is nothing left that can do the validation. So there must be something else that does the validation. So please add more information (logging, actual code, configuration etc.) as what you show here and explain should work (and has for many years). – M. Deinum Jul 13 '23 at 07:38

0 Answers0