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

This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-24 18:31:10 -04:00
1 parent ed21e67cd4
commit a16478cd91
7 files changed
+50 -21

No files matched your search

@@ -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: "
@@ -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) {
@@ -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();
@@ -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(
@@ -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);