Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

refactor(core): share Smithy length validation messages (#6449)

Andre Kuraita16478cd91c0
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/SmithyValidationErrors.java b/src/main/java/io/github/hectorvent/floci/core/common/SmithyValidationErrors.java
index bb33c6cf82..b74b55a719 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/SmithyValidationErrors.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/SmithyValidationErrors.java
@@ -80,6 +80,24 @@ public final class SmithyValidationErrors {
return null;
}
+ /** The violation for a string outside its length bounds, or {@code null} for one inside. */
+ public static String lengthOutOfRange(String path, String value, int min, int max) {
+ if (value.length() < min) {
+ return lengthTooShort(path, value, min);
+ }
+ if (value.length() > max) {
+ return "Value '" + value + "' at '" + path + "' failed to satisfy constraint: "
+ + "Member must have length less than or equal to " + max;
+ }
+ return null;
+ }
+
+ /** Also used by services that reject whitespace-only input as an empty member. */
+ public static String lengthTooShort(String path, String value, int min) {
+ return "Value '" + value + "' at '" + path + "' failed to satisfy constraint: "
+ + "Member must have length greater than or equal to " + min;
+ }
+
/** The violation for a value outside its enum, listing the allowed values in the model's order. */
public static String notInEnum(String value, String path, List<String> allowed) {
return "Value '" + value + "' at '" + path + "' failed to satisfy constraint: "
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java
index 1263f76afa..aa38fe3d1b 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java
@@ -371,8 +371,9 @@ public class CloudWatchMetricsService {
String name = alarm.getAlarmName();
if (name == null || name.isBlank() || name.length() > 255) {
throw SmithyValidationErrors.validationError(name == null ? SmithyValidationErrors.notNull("alarmName")
- : "Value '" + name + "' at 'alarmName' failed to satisfy constraint: Member must have length "
- + (name.length() > 255 ? "less than or equal to 255" : "greater than or equal to 1"));
+ : name.length() > 255
+ ? SmithyValidationErrors.lengthOutOfRange("alarmName", name, 1, 255)
+ : SmithyValidationErrors.lengthTooShort("alarmName", name, 1));
}
Alarm previous = alarmStore.get(region + "::" + name).orElse(null);
if (previous != null) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmRule.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmRule.java
index 8d2d97eff0..38a583016d 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmRule.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmRule.java
@@ -21,8 +21,9 @@ final class CompositeAlarmRule {
static Rule parse(String input) {
if (input == null || input.isBlank() || input.length() > 10240) {
throw SmithyValidationErrors.validationError(input == null ? SmithyValidationErrors.notNull("alarmRule")
- : "Value '" + input + "' at 'alarmRule' failed to satisfy constraint: Member must have length "
- + (input.length() > 10240 ? "less than or equal to 10240" : "greater than or equal to 1"));
+ : input.length() > 10240
+ ? SmithyValidationErrors.lengthOutOfRange("alarmRule", input, 1, 10240)
+ : SmithyValidationErrors.lengthTooShort("alarmRule", input, 1));
}
CompositeAlarmRule parser = new CompositeAlarmRule(input);
Expression expression = parser.or();
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbTableNames.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbTableNames.java
index 4c12d4bb5f..a66ae79e4c 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbTableNames.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbTableNames.java
@@ -34,8 +34,7 @@ public final class DynamoDbTableNames {
}
if (input.isEmpty()) {
throw SmithyValidationErrors.validationException(
- "Value '" + input
- + "' at 'tableName' failed to satisfy constraint: Member must have length greater than or equal to 3");
+ SmithyValidationErrors.lengthOutOfRange("tableName", input, 3, 255));
}
if (input.startsWith("arn:")) {
throw invalid("TableName must be a short name, not an ARN: " + input);
@@ -59,7 +58,7 @@ public final class DynamoDbTableNames {
}
if (input.isEmpty()) {
throw SmithyValidationErrors.validationException(
- "Value '' at 'tableName' failed to satisfy constraint: Member must have length greater than or equal to 1");
+ SmithyValidationErrors.lengthOutOfRange("tableName", input, 1, 255));
}
if (input.startsWith("arn:")) {
return parseArn(input);
@@ -113,13 +112,11 @@ public final class DynamoDbTableNames {
private static void validateTableNameForCreate(String tableName) {
if (tableName.length() < 3) {
throw SmithyValidationErrors.validationException(
- "Value '" + tableName
- + "' at 'tableName' failed to satisfy constraint: Member must have length greater than or equal to 3");
+ SmithyValidationErrors.lengthOutOfRange("tableName", tableName, 3, 255));
}
if (tableName.length() > 255) {
throw SmithyValidationErrors.validationException(
- "Value '" + tableName
- + "' at 'tableName' failed to satisfy constraint: Member must have length less than or equal to 255");
+ SmithyValidationErrors.lengthOutOfRange("tableName", tableName, 3, 255));
}
if (!TABLE_NAME_CHARS.matcher(tableName).matches()) {
throw SmithyValidationErrors.validationException(
@@ -132,13 +129,11 @@ public final class DynamoDbTableNames {
private static void validateTableNameForOps(String tableName) {
if (tableName.length() < 3) {
throw SmithyValidationErrors.validationException(
- "Value '" + tableName
- + "' at 'tableName' failed to satisfy constraint: Member must have length greater than or equal to 3");
+ SmithyValidationErrors.lengthOutOfRange("tableName", tableName, 3, 255));
}
if (tableName.length() > 255) {
throw SmithyValidationErrors.validationException(
- "Value '" + tableName
- + "' at 'tableName' failed to satisfy constraint: Member must have length less than or equal to 255");
+ SmithyValidationErrors.lengthOutOfRange("tableName", tableName, 3, 255));
}
if (!TABLE_NAME_CHARS.matcher(tableName).matches()) {
throw SmithyValidationErrors.validationException(
diff --git a/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java b/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java
index b56d85dda2..21e5765f33 100644
--- a/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java
@@ -1439,12 +1439,10 @@ public class EventBridgeService implements ResourceProvider {
+ "Member must have value greater than or equal to 0");
}
if (description != null && description.length() > 512) {
- violations.add("Value '" + description + "' at 'description' failed to satisfy constraint: "
- + "Member must have length less than or equal to 512");
+ violations.add(SmithyValidationErrors.lengthOutOfRange("description", description, 0, 512));
}
if (eventPattern != null && eventPattern.length() > 4096) {
- violations.add("Value '" + eventPattern + "' at 'eventPattern' failed to satisfy constraint: "
- + "Member must have length less than or equal to 4096");
+ violations.add(SmithyValidationErrors.lengthOutOfRange("eventPattern", eventPattern, 0, 4096));
}
if (!violations.isEmpty()) {
throw SmithyValidationErrors.validationException(violations);
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/SmithyValidationErrorsTest.java b/src/test/java/io/github/hectorvent/floci/core/common/SmithyValidationErrorsTest.java
index d6ce366076..72d49af967 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/SmithyValidationErrorsTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/SmithyValidationErrorsTest.java
@@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
class SmithyValidationErrorsTest {
@@ -49,4 +50,19 @@ class SmithyValidationErrorsTest {
void noViolations_isAProgrammingError() {
assertThrows(IllegalArgumentException.class, () -> SmithyValidationErrors.message(List.of()));
}
+ @Test
+ void lengthBoundsPreserveValuesAndIncludeEndpoints() {
+ assertEquals("Value '' at 'name' failed to satisfy constraint: "
+ + "Member must have length greater than or equal to 1",
+ SmithyValidationErrors.lengthOutOfRange("name", "", 1, 3));
+ assertEquals("Value 'abcd' at 'name' failed to satisfy constraint: "
+ + "Member must have length less than or equal to 3",
+ SmithyValidationErrors.lengthOutOfRange("name", "abcd", 1, 3));
+ assertNull(SmithyValidationErrors.lengthOutOfRange("name", "a", 1, 3));
+ assertNull(SmithyValidationErrors.lengthOutOfRange("name", "abc", 1, 3));
+ assertNull(SmithyValidationErrors.lengthOutOfRange("name", " ", 1, 3));
+ assertEquals("Value ' ' at 'name' failed to satisfy constraint: "
+ + "Member must have length greater than or equal to 1",
+ SmithyValidationErrors.lengthTooShort("name", " ", 1));
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmIntegrationTest.java
index d30ad431a2..7ed97dae6e 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmIntegrationTest.java
@@ -111,14 +111,14 @@ class CompositeAlarmIntegrationTest {
for (String field : List.of("AlarmName", "AlarmRule")) {
int maximum = "AlarmName".equals(field) ? 255 : 10240;
String path = "AlarmName".equals(field) ? "alarmName" : "alarmRule";
- for (String value : List.of("", "x".repeat(maximum + 1))) {
+ for (String value : List.of("", " ", "x".repeat(maximum + 1), " ".repeat(maximum + 1))) {
Map<String, String> request = new LinkedHashMap<>(Map.of("AlarmName", "length-validation", "AlarmRule", "TRUE"));
request.put(field, value);
Response error = call(json, "PutCompositeAlarm", request);
error.then().statusCode(400);
assertEquals("1 validation error detected: Value '" + value + "' at '" + path
+ "' failed to satisfy constraint: Member must have length "
- + (value.isEmpty() ? "greater than or equal to 1" : "less than or equal to " + maximum),
+ + (value.length() <= maximum ? "greater than or equal to 1" : "less than or equal to " + maximum),
errorMessage(json, error));
}
}