|
@@ -0,0 +1,28 @@
|
|
|
+package org.thyy.utils;
|
|
|
+
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import jakarta.validation.ConstraintViolation;
|
|
|
+import jakarta.validation.Validator;
|
|
|
+
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+@Component
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class ValidatorUtils {
|
|
|
+
|
|
|
+ private final Validator validator;
|
|
|
+
|
|
|
+ public void validate(Object object) {
|
|
|
+ Set<ConstraintViolation<Object>> violations = validator.validate(object);
|
|
|
+
|
|
|
+ if (!violations.isEmpty()) {
|
|
|
+ StringBuilder errorMsg = new StringBuilder("Validation failed:");
|
|
|
+ for (ConstraintViolation<Object> violation : violations) {
|
|
|
+ errorMsg.append("\n").append(violation.getMessage());
|
|
|
+ }
|
|
|
+ throw new IllegalArgumentException(errorMsg.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|