Search in sources :

Example 1 with InvalidDataException

use of com.flytecnologia.core.exception.InvalidDataException in project flytecnologia-api by jullierme.

the class FlyValidatorUtil method validate.

public static void validate(Object entry, Class<?> targetClass, String methodName) throws MethodArgumentNotValidException {
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(entry, entry.getClass().getName());
    validator.validate(entry, errors);
    if (errors.hasErrors()) {
        Method method = null;
        for (Method methodAux : targetClass.getMethods()) {
            if (methodAux.getName().equals(methodName)) {
                method = methodAux;
                break;
            }
        }
        if (method != null) {
            MethodParameter methodParameter = new MethodParameter(method, 0);
            throw new MethodArgumentNotValidException(methodParameter, errors);
        } else {
            throw new InvalidDataException(errors.getAllErrors().toString(), errors);
        }
    }
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) InvalidDataException(com.flytecnologia.core.exception.InvalidDataException) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException)

Aggregations

InvalidDataException (com.flytecnologia.core.exception.InvalidDataException)1 Method (java.lang.reflect.Method)1 MethodParameter (org.springframework.core.MethodParameter)1 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)1 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)1