-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to configure timestamp validation
This commit updates the Node validation visitor to allow for a configurable strategy for validating timestamps. This is needed in order to validate that the timestamps provided for protocol tests are always in epoch seconds.
- Loading branch information
Showing
6 changed files
with
162 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...c/main/java/software/amazon/smithy/model/validation/node/TimestampValidationStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.model.validation.node; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import software.amazon.smithy.model.node.Node; | ||
import software.amazon.smithy.model.shapes.MemberShape; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
import software.amazon.smithy.model.shapes.ShapeIndex; | ||
import software.amazon.smithy.utils.ListUtils; | ||
|
||
/** | ||
* Defines how timestamps are validated. | ||
*/ | ||
public enum TimestampValidationStrategy implements NodeValidatorPlugin { | ||
/** | ||
* Validates timestamps by requiring that the value uses matches the | ||
* resolved timestamp format, or is a unix timestamp or integer in the | ||
* case that a member or shape does not have a {@code timestampFormat} | ||
* trait. | ||
*/ | ||
FORMAT { | ||
@Override | ||
public List<String> apply(Shape shape, Node value, ShapeIndex index) { | ||
return new TimestampFormatPlugin().apply(shape, value, index); | ||
} | ||
}, | ||
|
||
/** | ||
* Requires that the value provided for all timestamp shapes is a | ||
* unix timestamp. | ||
*/ | ||
EPOCH_SECONDS { | ||
@Override | ||
public List<String> apply(Shape shape, Node value, ShapeIndex index) { | ||
if (isTimestampMember(index, shape) && !value.isNumberNode()) { | ||
return ListUtils.of("Invalid " + value.getType() + " value provided for timestamp, `" | ||
+ shape.getId() + "`. Expected a number that contains epoch seconds " | ||
+ "with optional millisecond precision"); | ||
} else { | ||
return Collections.emptyList(); | ||
} | ||
} | ||
}; | ||
|
||
private static boolean isTimestampMember(ShapeIndex model, Shape shape) { | ||
return shape.asMemberShape() | ||
.map(MemberShape::getTarget) | ||
.flatMap(model::getShape) | ||
.filter(Shape::isTimestampShape) | ||
.isPresent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
21 changes: 21 additions & 0 deletions
21
...ources/software/amazon/smithy/protocoltests/traits/errorfiles/timestamp-validation.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace smithy.example | ||
|
||
use smithy.test#httpRequestTests | ||
|
||
@http(method: "POST", uri: "/") | ||
@httpRequestTests([ | ||
{ | ||
id: "foo3", | ||
protocol: "example", | ||
method: "POST", | ||
uri: "/", | ||
params: { | ||
time: 946845296 | ||
} | ||
} | ||
]) | ||
operation HasTime(HasTimeInput) | ||
|
||
structure HasTimeInput { | ||
time: Timestamp, | ||
} |