All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.breuninger.boot.validation.web.BindExceptionValidator.kt Maven / Gradle / Ivy

There is a newer version: 3.2.1.RELEASE
Show newest version
package com.breuninger.boot.validation.web

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.stereotype.Component
import org.springframework.validation.BeanPropertyBindingResult
import org.springframework.validation.BindException
import org.springframework.validation.Validator

@Component
@ConditionalOnProperty(prefix = "breuni.validation", name = ["enabled"], havingValue = "true")
class BindExceptionValidator(private val webFluxValidator: Validator) {

  fun validate(target: Any): BeanPropertyBindingResult {
    val bindingResult = BeanPropertyBindingResult(target, target.javaClass.simpleName.toLowerCase())
    webFluxValidator.validate(target, bindingResult)
    return bindingResult
  }

  @Throws(BindException::class)
  fun validateAndThrowException(target: Any) {
    val bindingResult = validate(target)
    if (bindingResult.hasErrors()) {
      throw BindException(bindingResult)
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy