Skip to content

Commit 496c074

Browse files
committed
Refactor JavalidationException and ValidationErrors for improved constructing
1 parent 8912c5b commit 496c074

File tree

8 files changed

+27
-28
lines changed

8 files changed

+27
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ public ResponseEntity<?> createOrder(@RequestBody OrderRequest request) {
671671
// In repository layer
672672
public User findByIdOrThrow(Long id) {
673673
return userRepository.findById(id)
674-
.orElseThrow(() -> new JavalidationException("User not found"));
674+
.orElseThrow(() -> JavalidationException.ofRoot("User not found"));
675675
}
676676

677677
// In business logic

javalidation/src/main/java/io/github/raniagus/javalidation/JavalidationException.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
* <b>Creating directly:</b>
3030
* <pre>{@code
3131
* // Single root error
32-
* throw new JavalidationException("Invalid request");
32+
* throw JavalidationException.ofRoot("Invalid request");
3333
*
3434
* // Single field error
35-
* throw new JavalidationException("email", "Invalid email format");
35+
* throw JavalidationException.ofField("email", "Invalid email format");
3636
*
3737
* // From accumulated errors
3838
* throw new JavalidationException(validationErrors);
@@ -63,8 +63,8 @@ public JavalidationException(ValidationErrors errors) {
6363
* @param message the error message template
6464
* @param args arguments for the message template
6565
*/
66-
public JavalidationException(String message, Object... args) {
67-
this(ValidationErrors.of(message, args));
66+
public static JavalidationException ofRoot(String message, Object... args) {
67+
return new JavalidationException(ValidationErrors.ofRoot(message, args));
6868
}
6969

7070
/**
@@ -76,8 +76,8 @@ public JavalidationException(String message, Object... args) {
7676
* @param message the error message template
7777
* @param args arguments for the message template
7878
*/
79-
public JavalidationException(String field, String message, Object... args) {
80-
this(ValidationErrors.of(field, message, args));
79+
public static JavalidationException ofField(String field, String message, Object... args) {
80+
return new JavalidationException(ValidationErrors.ofField(field, message, args));
8181
}
8282

8383
/**

javalidation/src/main/java/io/github/raniagus/javalidation/Result.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ default T getOrElse(Supplier<T> supplier) {
779779
* @return an {@link Err} result containing the error
780780
*/
781781
static <T extends @Nullable Object> Result<T> err(String message) {
782-
return new Err<>(ValidationErrors.of(message));
782+
return new Err<>(ValidationErrors.ofRoot(message));
783783
}
784784

785785
/**
@@ -797,7 +797,7 @@ default T getOrElse(Supplier<T> supplier) {
797797
* @return an {@link Err} result containing the field error
798798
*/
799799
static <T extends @Nullable Object> Result<T> err(String field, String message) {
800-
return new Err<>(ValidationErrors.of(field, message));
800+
return new Err<>(ValidationErrors.ofField(field, message));
801801
}
802802

803803
/**

javalidation/src/main/java/io/github/raniagus/javalidation/ValidationErrors.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.github.raniagus.javalidation;
22

33
import io.github.raniagus.javalidation.format.TemplateString;
4-
import java.util.ArrayList;
54
import java.util.HashMap;
65
import java.util.List;
76
import java.util.Map;
@@ -101,7 +100,7 @@ public static ValidationErrors empty() {
101100
* @param args optional arguments for the message template
102101
* @return validation errors containing the single root error
103102
*/
104-
public static ValidationErrors of(String message, Object... args) {
103+
public static ValidationErrors ofRoot(String message, Object... args) {
105104
return new ValidationErrors(List.of(new TemplateString(message, args)), Map.of());
106105
}
107106

@@ -118,7 +117,7 @@ public static ValidationErrors of(String message, Object... args) {
118117
* @param args optional arguments for the message template
119118
* @return validation errors containing the single field error
120119
*/
121-
public static ValidationErrors of(String field, String message, Object... args) {
120+
public static ValidationErrors ofField(String field, String message, Object... args) {
122121
return new ValidationErrors(List.of(), Map.of(field, List.of(new TemplateString(message, args))));
123122
}
124123

javalidation/src/test/java/io/github/raniagus/javalidation/JavalidationExceptionTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ class JavalidationExceptionTest {
88

99
@Test
1010
void givenSingleFieldError_whenGetMessage_thenReturnsErrorCount() {
11-
var exception = new JavalidationException("email", "Invalid email format");
11+
var exception = JavalidationException.ofField("email", "Invalid email format");
1212

1313
assertThat(exception.getMessage()).isEqualTo("Validation failed with 1 error(s)");
1414
}
1515

1616
@Test
1717
void givenSingleRootError_whenGetMessage_thenReturnsErrorCount() {
18-
var exception = new JavalidationException("Something went wrong");
18+
var exception = JavalidationException.ofRoot("Something went wrong");
1919

2020
assertThat(exception.getMessage()).isEqualTo("Validation failed with 1 error(s)");
2121
}
@@ -35,7 +35,7 @@ void givenMultipleErrors_whenGetMessage_thenReturnsTotalCount() {
3535

3636
@Test
3737
void givenException_whenGetErrors_thenReturnsValidationErrors() {
38-
var errors = ValidationErrors.of("email", "Invalid format");
38+
var errors = ValidationErrors.ofField("email", "Invalid format");
3939
var exception = new JavalidationException(errors);
4040

4141
assertThat(exception.getErrors()).isEqualTo(errors);
@@ -50,7 +50,7 @@ void givenEmptyErrors_whenGetMessage_thenReturnsZeroCount() {
5050

5151
@Test
5252
void givenFieldErrorWithArgs_whenGetMessage_thenReturnsErrorCount() {
53-
var exception = new JavalidationException("age", "Must be at least {0}", 18);
53+
var exception = JavalidationException.ofField("age", "Must be at least {0}", 18);
5454

5555
assertThat(exception.getMessage()).isEqualTo("Validation failed with 1 error(s)");
5656
}

javalidation/src/test/java/io/github/raniagus/javalidation/ResultTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void givenSupplierReturningValue_whenOf_thenReturnsOk() {
1919
@Test
2020
void givenSupplierThrowingValidationException_whenOf_thenReturnsErr() {
2121
var result = Result.of(() -> {
22-
throw new JavalidationException(ValidationErrors.of("error"));
22+
throw new JavalidationException(ValidationErrors.ofRoot("error"));
2323
});
2424

2525
assertThatThrownBy(result::getOrThrow)
@@ -217,7 +217,7 @@ void givenErr_whenMap_thenPreservesError() {
217217
@Test
218218
void givenOk_whenMapThrowsJavalidationException_thenCatchesAndReturnsErr() {
219219
var result = Result.ok(5).map(x -> {
220-
throw new JavalidationException("error in mapper");
220+
throw JavalidationException.ofRoot("error in mapper");
221221
});
222222

223223
assertThat(result).isInstanceOf(Result.Err.class);
@@ -353,7 +353,7 @@ void givenErr_whenFlatMap_thenSkipsFunctionAndPreservesError() {
353353
@Test
354354
void givenOk_whenFlatMapThrowsJavalidationException_thenCatchesAndReturnsErr() {
355355
var result = Result.ok(5).flatMap(x -> {
356-
throw new JavalidationException("error in flatMap");
356+
throw JavalidationException.ofRoot("error in flatMap");
357357
});
358358

359359
assertThat(result).isInstanceOf(Result.Err.class);
@@ -549,6 +549,6 @@ void givenOkAndFailingPredicate_whenFilterWithField_thenReturnsFieldErr() {
549549
}
550550

551551
private void raiseJavalidationException() {
552-
throw new JavalidationException("error");
552+
throw JavalidationException.ofRoot("error");
553553
}
554554
}

javalidation/src/test/java/io/github/raniagus/javalidation/ValidationErrorsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ValidationErrorsTest {
1111

1212
@Test
1313
void givenRootErrors_whenWithPrefix_thenConvertsToFieldErrors() {
14-
var errors = ValidationErrors.of("root error");
14+
var errors = ValidationErrors.ofRoot("root error");
1515

1616
var prefixed = errors.withPrefix("user");
1717

@@ -22,7 +22,7 @@ void givenRootErrors_whenWithPrefix_thenConvertsToFieldErrors() {
2222

2323
@Test
2424
void givenFieldErrors_whenWithPrefix_thenPrefixesFieldNames() {
25-
var errors = ValidationErrors.of("email", "invalid");
25+
var errors = ValidationErrors.ofField("email", "invalid");
2626

2727
var prefixed = errors.withPrefix("form");
2828

@@ -34,7 +34,7 @@ void givenFieldErrors_whenWithPrefix_thenPrefixesFieldNames() {
3434

3535
@Test
3636
void givenFieldErrors_whenWithPrefixVarargs_thenBuildsPrefix() {
37-
var errors = ValidationErrors.of("field", "error");
37+
var errors = ValidationErrors.ofField("field", "error");
3838

3939
var prefixed = errors.withPrefix("parent", ".", "child");
4040

javalidation/src/test/java/io/github/raniagus/javalidation/ValidationTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void givenNullMessage_whenAddFieldError_thenThrowsNullPointerException() {
9696

9797
@Test
9898
void givenValidationErrors_whenAddAll_thenAddsAllErrors() {
99-
var validationErrors = ValidationErrors.of("field", "error");
99+
var validationErrors = ValidationErrors.ofField("field", "error");
100100
var validation = Validation.create()
101101
.addAll(validationErrors);
102102

@@ -116,7 +116,7 @@ void givenNull_whenAddAll_thenThrowsNullPointerException() {
116116

117117
@Test
118118
void givenFieldErrors_whenAddAllWithPrefix_thenPrefixesFieldNames() {
119-
var validationErrors = ValidationErrors.of("field", "error");
119+
var validationErrors = ValidationErrors.ofField("field", "error");
120120
var validation = Validation.create()
121121
.addAll("root", validationErrors);
122122

@@ -126,7 +126,7 @@ void givenFieldErrors_whenAddAllWithPrefix_thenPrefixesFieldNames() {
126126

127127
@Test
128128
void givenRootErrors_whenAddAllWithPrefix_thenConvertsToFieldErrors() {
129-
var validationErrors = ValidationErrors.of("root error");
129+
var validationErrors = ValidationErrors.ofRoot("root error");
130130
var validation = Validation.create()
131131
.addAll("prefix", validationErrors);
132132

@@ -137,7 +137,7 @@ void givenRootErrors_whenAddAllWithPrefix_thenConvertsToFieldErrors() {
137137
@Test
138138
void givenNullPrefix_whenAddAllWithPrefix_thenThrowsNullPointerException() {
139139
var validation = Validation.create();
140-
var validationErrors = ValidationErrors.of("error");
140+
var validationErrors = ValidationErrors.ofRoot("error");
141141

142142
assertThatThrownBy(() -> validation.addAll(null, validationErrors))
143143
.isInstanceOf(NullPointerException.class);
@@ -196,7 +196,7 @@ void givenSupplierThrows_whenAsResult_thenReturnsErr() {
196196
var validation = Validation.create();
197197

198198
var result = validation.asResult(() -> {
199-
throw new JavalidationException(ValidationErrors.of("error"));
199+
throw new JavalidationException(ValidationErrors.ofRoot("error"));
200200
});
201201
assertThatThrownBy(result::getOrThrow)
202202
.isInstanceOf(JavalidationException.class);

0 commit comments

Comments
 (0)