Skip to content

Commit ffac280

Browse files
committed
feat: Validate number format 'float' for 32-bit overflow
1 parent 9bdc120 commit ffac280

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/main/java/com/retailsvc/http/validate/DefaultValidator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ private record NumberFormatCheck(DoublePredicate isValid, String message) {}
8989
"int64",
9090
new IntegerFormatCheck(n -> true, "value does not fit in int64"));
9191

92-
private static final Map<String, NumberFormatCheck> NUMBER_FORMAT_CHECKS = Map.of();
92+
private static final Map<String, NumberFormatCheck> NUMBER_FORMAT_CHECKS =
93+
Map.of(
94+
"float",
95+
new NumberFormatCheck(
96+
n -> !Double.isNaN(n) && !Double.isInfinite(n) && Math.abs(n) <= Float.MAX_VALUE,
97+
"value does not fit in float"));
9398

9499
private final Function<String, Schema> refResolver;
95100
private final ConcurrentMap<String, Pattern> compiledPatterns = new ConcurrentHashMap<>();

src/test/java/com/retailsvc/http/validate/StringIntegerNumberTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,25 @@ void integerFormatInt32() {
265265
.extracting(t -> ((ValidationException) t).error().keyword())
266266
.isEqualTo("format");
267267
}
268+
269+
@Test
270+
void numberFormatFloat() {
271+
NumberSchema s =
272+
new NumberSchema(Set.of(TypeName.NUMBER), null, null, null, null, null, "float");
273+
assertThatCode(() -> v.validate(1.5, s, "/v")).doesNotThrowAnyException();
274+
assertThatCode(() -> v.validate(-1.5, s, "/v")).doesNotThrowAnyException();
275+
assertThatCode(() -> v.validate((double) Float.MAX_VALUE, s, "/v")).doesNotThrowAnyException();
276+
assertThatThrownBy(() -> v.validate(1e40, s, "/v"))
277+
.extracting(t -> ((ValidationException) t).error().keyword())
278+
.isEqualTo("format");
279+
assertThatThrownBy(() -> v.validate(-1e40, s, "/v"))
280+
.extracting(t -> ((ValidationException) t).error().keyword())
281+
.isEqualTo("format");
282+
assertThatThrownBy(() -> v.validate(Double.NaN, s, "/v"))
283+
.extracting(t -> ((ValidationException) t).error().keyword())
284+
.isEqualTo("format");
285+
assertThatThrownBy(() -> v.validate(Double.POSITIVE_INFINITY, s, "/v"))
286+
.extracting(t -> ((ValidationException) t).error().keyword())
287+
.isEqualTo("format");
288+
}
268289
}

0 commit comments

Comments
 (0)