Skip to content

Commit

Permalink
Merge branch 'release/4.2.2.RELEASE.20220613'
Browse files Browse the repository at this point in the history
  • Loading branch information
JesusHdez960717 committed Jun 14, 2022
2 parents 45d4fb5 + 2e7e3ad commit caac921
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {

group = 'dev.root101.clean'

version = '4.2.1.RELEASE.20220531'
version = '4.2.2.RELEASE.20220613'

repositories {
jcenter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

/**
* La idea es almacenar todas las excepciones o similar como un stack trace en
* la bd para acceder remoto. De igual manera ese módulo recivir posibles
* errores del cliente
* la bd para acceder remoto.
*
* @author Root101 ([email protected], +53-5-426-8660)
* @author JesusHdezWaterloo@Github
Expand All @@ -39,7 +38,7 @@ public static ExceptionHandlerService registerExceptionService(ExceptionHandlerS
Objects.requireNonNull(newService, "ExceptionHandlerService can't be null");

exceptionServices.add(newService);

return newService;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final void addHandler(Consumer<Throwable> consumer, Class... types) {
@Override
public void handleException(Throwable ex) {
Class type = ex.getClass();

exceptionsMap.forEach((t, u) -> {
if (t.isAssignableFrom(type)) {
u.accept(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package dev.root101.clean.core.exceptions;

import org.springframework.http.HttpStatus;;
import org.springframework.http.HttpStatus;

;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package dev.root101.clean.core.exceptions;

import org.springframework.http.HttpStatus;;
import org.springframework.http.HttpStatus;

;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package dev.root101.clean.core.exceptions;

import org.springframework.http.HttpStatus;;
import org.springframework.http.HttpStatus;

;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package dev.root101.clean.core.exceptions;

import org.springframework.http.HttpStatus;;
import org.springframework.http.HttpStatus;

;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package dev.root101.clean.core.exceptions;

import org.springframework.http.HttpStatus;;
import org.springframework.http.HttpStatus;

;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package dev.root101.clean.core.exceptions;

import org.springframework.http.HttpStatus;;
import org.springframework.http.HttpStatus;

;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package dev.root101.clean.core.exceptions;

import org.springframework.http.HttpStatus;;
import org.springframework.http.HttpStatus;

;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package dev.root101.clean.core.exceptions;

import org.springframework.http.HttpStatus;;
import org.springframework.http.HttpStatus;

;

/**
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.root101.clean.core.exceptions;

import java.util.Arrays;
import java.util.List;
import org.springframework.http.HttpStatus;

Expand Down Expand Up @@ -30,4 +31,9 @@ public record ValidationErrorMessage(
String mensaje) {

}

@Override
public String toString() {
return "ValidationException{" + "statusCode=" + statusCode + ", messages=" + Arrays.toString(messages.toArray()) + '}';
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,111 @@
package dev.root101.clean.core.utils.validation;

import dev.root101.clean.core.exceptions.ValidationException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.validation.Configuration;
import javax.validation.ConstraintViolation;
import javax.validation.MessageInterpolator;
import javax.validation.Validation;
import javax.validation.Validator;
import org.springframework.util.ClassUtils;

public class ValidationService {

//store the config
private static final Configuration<?> CONFIG = Validation.byDefaultProvider().configure();

//Created static to avoid recreated every time a validation occur
private static final Validator DEFAULT_VALIDATOR = javax.validation.Validation.buildDefaultValidatorFactory().getValidator();
private static Validator DEFAULT_VALIDATOR = CONFIG.buildValidatorFactory().getValidator();

public static Set<ConstraintViolation<Object>> validate(Object object) {
return DEFAULT_VALIDATOR.validate(object);
public static MessageInterpolator defaultMessageInterpolator() {
return CONFIG.getDefaultMessageInterpolator();
}

public static void validateAndThrow(Object... objects) throws ValidationException {
public static void setMessageInterpolator(MessageInterpolator msgInterp) {
DEFAULT_VALIDATOR = CONFIG.messageInterpolator(
msgInterp
).buildValidatorFactory().getValidator();
}

public static List<ConstraintViolation<Object>> validate(Object... objects) {
List<ConstraintViolation<Object>> errors = new ArrayList<>();
List.of(objects).forEach((object) -> {

for (Object object : objects) {
Set<ConstraintViolation<Object>> aaa = DEFAULT_VALIDATOR.validate(object);
errors.addAll(aaa);
});
}

return errors;
}

public static List<ConstraintViolation<Object>> validateRecursive(Object... objects) {
return validateRecursive(new ArrayList<>(), objects);
}

public static void validateAndThrow(Object... objects) throws ValidationException {
List<ConstraintViolation<Object>> errors = validate(objects);
if (!errors.isEmpty()) {
throw new ValidationException(convertMessages(errors));
}
}

public static void validateRecursiveAndThrow(Object... objects) throws ValidationException {
List<ConstraintViolation<Object>> errors = validateRecursive(objects);
if (!errors.isEmpty()) {
throw new ValidationException(convertMessages(errors));
}
}

private static List<ValidationException.ValidationErrorMessage> convertMessages(List<ConstraintViolation<Object>> violation) {
public static List<ValidationException.ValidationErrorMessage> convertMessages(List<ConstraintViolation<Object>> violation) {
return violation.stream().map((viol) -> {
return new ValidationException.ValidationErrorMessage(viol.getPropertyPath().toString(), viol.getInvalidValue().toString(), viol.getMessage());
return new ValidationException.ValidationErrorMessage(viol.getPropertyPath().toString(), viol.getInvalidValue() == null ? "null" : viol.getInvalidValue().toString(), viol.getMessage());
}).collect(Collectors.toList());
}

private static List<ConstraintViolation<Object>> validateRecursive(List<ConstraintViolation<Object>> currentViolations, Object... objects) {
//recorro la lista de objetos a validar
for (Object object : objects) {
//si NO es null lo proceso. Null no tiene sentido validarlo(DEFAULT_VALIDATOR.validate(null) lanza excepcion), se deberia haber validado una capa arriba.
if (object != null) {
//si no es null compruebo si:
if (object instanceof Object[] arr) {
//es una instancia de arreglo, llamo a la recursividad con el arreglo
validateRecursive(currentViolations, arr);
} else if (object instanceof List list) {
//es una instancia de lista, la convierto en arreglo y llamo a la recursividad con el arreglo
validateRecursive(currentViolations, list.toArray());
} else {
//no es ni una lista ni un arreglo, valido el objeto como objeto
currentViolations.addAll(DEFAULT_VALIDATOR.validate(object));

//luego recorro todos sus campos a ver si alguno no es primitivo
Field fields[] = object.getClass().getDeclaredFields();
Object[] arr
= List.of(fields).stream()
//convierto los field en el objeto como tal
.map((field) -> {
field.setAccessible(true);
try {
return field.get(object);
} catch (IllegalAccessException | IllegalArgumentException e) {
System.out.println("Nunca debe entrar aqui");
}
return null;
})
//filtro de todos los valores los que NO sean primitivos
.filter((t) -> {
return t != null && !(ClassUtils.isPrimitiveOrWrapper(t.getClass()) || t instanceof String);
})
.toArray();
//de todos los campos que no son promitivos los valido
validateRecursive(currentViolations, arr);
}
}
}
return currentViolations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import dev.root101.clean.core.utils.validation.checkables.CheckerFactory;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
*
* @author Root101 ([email protected], +53-5-426-8660)
Expand Down

0 comments on commit caac921

Please sign in to comment.