Hi chat! i need help! please I have a class ```da...
# spring
s
Hi chat! i need help! please I have a class
Copy code
data class EmailVerificationStateRequestDto (
    @field:NotBlank(message = "parameterName не указан")
    @field:Size(min = 6, max = 6, message = "parameterName должен содержать 6 алфавитно-цифровых символов" )
    @field:Pattern(regexp = "^[a-zA-Z0-9]+\$")
    val parameterName: String?,
    val email: String?
)
three validation for one field and i have this class where i can get some information about error p.s. service vy webFlux, Mono, Flux, Reactive
Copy code
@ExceptionHandler(WebExchangeBindException::class)
fun handleException(bindException: WebExchangeBindException): ResponseEntity<String> {
    val validationError = ValidationError(
        bindException.fieldError?.defaultMessage
            ?: throw RuntimeException("Something wrong with validation error message")
    )
    return ResponseEntity.badRequest().body(objectMapper.writeValueAsString(validationError))
}
when i call controller method and set parameterName = “” i got 3 errors, i want get 1 error NotBlank only, not all 3 validations, how can i do this?
t
I don’t think you can, the beauty of jakarta validation framework is that it collects all errors. in this particular example, you can write a proper regex that validates non blank, size and allowed characters at once, so you can drop notblank and size annotations, but in general I don’t think it is possible to automatically ignore certains validations
ofc you can have your own priority logic and exclude some errors if others are present, I am just saying I don’t think you can do it automatically
👌🏻 1
s
Yes. If I want do my business logic. Validator custom it is ok. But automatically no.
Thank you
n
You can do that but you will have to do something like this. https://github.com/ideasbucketlabs/tansen/blob/main/backend/main/java/com/ideasbucket/tansen/util/FormError.java And then
Copy code
@ExceptionHandler(WebExchangeBindException::class)
    suspend fun handleValidationError(exception: WebExchangeBindException): ResponseEntity<Response> {
        if ((exception.reason == null) || (exception.reason != "Validation failure") || !exception.hasFieldErrors()) {
            return getHttpResponseWithoutDetail(HttpStatus.BAD_REQUEST, exception.message)
        }
if ((exception.reason == "Validation failure") && (exception.bindingResult.fieldErrors.isNotEmpty())) {
            return ResponseEntity(
                Response.withError(FormError.format(exception.bindingResult.fieldErrors)),
                HttpStatus.UNPROCESSABLE_ENTITY
            )
        }
return getHttpResponseWithoutDetail(HttpStatus.UNPROCESSABLE_ENTITY, exception.message)
    }
Sorry code was long so I pasted the link instead but it should help.
🖐🏻 1
s
Thanks
n
Sure no problem. WebExceptionHandler is in that repo too.
Another option is to use
GroupSequence