-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/4.2.2.RELEASE.20220613'
- Loading branch information
Showing
14 changed files
with
118 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -39,7 +38,7 @@ public static ExceptionHandlerService registerExceptionService(ExceptionHandlerS | |
Objects.requireNonNull(newService, "ExceptionHandlerService can't be null"); | ||
|
||
exceptionServices.add(newService); | ||
|
||
return newService; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 83 additions & 8 deletions
91
src/main/java/dev/root101/clean/core/utils/validation/ValidationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|