Skip to content

Commit

Permalink
Adds additional event id subparts to DefaultTrait violations that pre…
Browse files Browse the repository at this point in the history
…viously collided (#1780)
  • Loading branch information
rchache authored May 22, 2023
1 parent fc0cf41 commit ee4977b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -428,14 +428,14 @@ private ValidationEvent event(

private List<ValidationEvent> applyPlugins(Shape shape) {
List<ValidationEvent> events = new ArrayList<>();
timestampValidationStrategy.apply(shape, value, validationContext, (location, severity, message) -> {
events.add(event(message, severity, location.getSourceLocation()));
});
timestampValidationStrategy.apply(shape, value, validationContext,
(location, severity, message, additionalEventIdParts) ->
events.add(event(message, severity, location.getSourceLocation(), additionalEventIdParts)));

for (NodeValidatorPlugin plugin : BUILTIN) {
plugin.apply(shape, value, validationContext, (location, severity, message) -> {
events.add(event(message, severity, location.getSourceLocation()));
});
plugin.apply(shape, value, validationContext,
(location, severity, message, additionalEventIdParts) ->
events.add(event(message, severity, location.getSourceLocation(), additionalEventIdParts)));
}

return events;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*/
@SmithyInternalApi
public interface NodeValidatorPlugin {
String[] EMPTY_STRING_ARRAY = new String[0];

/**
* Applies the plugin to the given shape, node value, and model.
Expand Down Expand Up @@ -125,10 +126,17 @@ public boolean hasFeature(NodeValidationVisitor.Feature feature) {
@SmithyInternalApi
@FunctionalInterface
interface Emitter {
void accept(FromSourceLocation sourceLocation, Severity severity, String message);
void accept(FromSourceLocation sourceLocation,
Severity severity,
String message,
String... additionalEventIdParts);

default void accept(FromSourceLocation sourceLocation, String message) {
accept(sourceLocation, Severity.ERROR, message);
accept(sourceLocation, Severity.ERROR, message, EMPTY_STRING_ARRAY);
}

default void accept(FromSourceLocation sourceLocation, Severity severity, String message) {
accept(sourceLocation, severity, message, EMPTY_STRING_ARRAY);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
* Validates the range trait on number shapes or members that target them.
*/
class RangeTraitPlugin implements NodeValidatorPlugin {
private static final String MEMBER = "Member";
private static final String TARGET = "Target";

@Override
public final void apply(Shape shape, Node value, Context context, Emitter emitter) {
Expand All @@ -55,13 +57,15 @@ private void checkNonNumeric(Shape shape, RangeTrait trait, StringNode node, Emi
if (trait.getMin().isPresent() && value.equals(NonNumericFloat.NEGATIVE_INFINITY)) {
emitter.accept(node, Severity.ERROR, String.format(
"Value provided for `%s` must be greater than or equal to %s, but found \"%s\"",
shape.getId(), trait.getMin().get(), node.getValue()));
shape.getId(), trait.getMin().get(), node.getValue()),
shape.isMemberShape() ? MEMBER : TARGET);
}

if (trait.getMax().isPresent() && value.equals(NonNumericFloat.POSITIVE_INFINITY)) {
emitter.accept(node, Severity.ERROR, String.format(
"Value provided for `%s` must be less than or equal to %s, but found \"%s\"",
shape.getId(), trait.getMax().get(), node.getValue()));
shape.getId(), trait.getMax().get(), node.getValue()),
shape.isMemberShape() ? MEMBER : TARGET);
}
});
}
Expand All @@ -76,15 +80,17 @@ protected void check(Shape shape, boolean zeroValueWarning, RangeTrait trait, Nu
if (decimal.compareTo(new BigDecimal(min.toString())) < 0) {
emitter.accept(node, getSeverity(node, zeroValueWarning), String.format(
"Value provided for `%s` must be greater than or equal to %s, but found %s",
shape.getId(), min, number));
shape.getId(), min, number),
shape.isMemberShape() ? MEMBER : TARGET);
}
});

trait.getMax().ifPresent(max -> {
if (decimal.compareTo(new BigDecimal(max.toString())) > 0) {
emitter.accept(node, getSeverity(node, zeroValueWarning), String.format(
"Value provided for `%s` must be less than or equal to %s, but found %s",
shape.getId(), max, number));
shape.getId(), max, number),
shape.isMemberShape() ? MEMBER : TARGET);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
[WARNING] smithy.example#MinOne: Error validating @default trait: Value provided for `smithy.example#MinOne` must be greater than or equal to 1, but found 0 | DefaultTrait
[WARNING] smithy.example#MinAndMax: Error validating @default trait: Value provided for `smithy.example#MinAndMax` must be greater than or equal to 1, but found 0 | DefaultTrait
[WARNING] smithy.example#MaxNegativeOne: Error validating @default trait: Value provided for `smithy.example#MaxNegativeOne` must be less than or equal to -1, but found 0 | DefaultTrait
[WARNING] smithy.example#Integers$doublyInvalidDefault: Error validating @default trait: Value provided for `smithy.example#Integers$doublyInvalidDefault` must be greater than or equal to 1, but found 0 | DefaultTrait.Member
[WARNING] smithy.example#Integers$doublyInvalidDefault: Error validating @default trait: Value provided for `smithy.example#MinOne` must be greater than or equal to 1, but found 0 | DefaultTrait.Target
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
$version: "1.0"
$version: "2.0"

namespace smithy.example

structure Integers {
@default(0)
noRange: PrimitiveInteger,

@range(min: 0, max: 1)
@default(0)
valid: PrimitiveInteger,

@range(min: 1)
@default(0)
invalidMin: PrimitiveInteger,

@range(max: -1, min: -100)
@default(0)
invalidMax: PrimitiveInteger,

@default(0)
invalidTargetMin: MinOne,

@default(0)
invalidTagetMax: MaxNegativeOne,

invalidMinWithMax: MinAndMax
@default(0)
invalidMinWithMax: MinAndMax,

@range(min: 1)
@default(0)
doublyInvalidDefault: MinOne
}

@range(min: 1)
@default(0)
integer MinOne

@range(min: 1, max: 100)
@default(0)
integer MinAndMax

@range(max: -1)
@default(0)
integer MaxNegativeOne

0 comments on commit ee4977b

Please sign in to comment.