-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #217 from JordonPhillips/minmax
Add validation for length trait values
- Loading branch information
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...rc/main/java/software/amazon/smithy/model/validation/validators/LengthTraitValidator.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,58 @@ | ||
/* | ||
* 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.validators; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
import software.amazon.smithy.model.traits.LengthTrait; | ||
import software.amazon.smithy.model.traits.Trait; | ||
import software.amazon.smithy.model.validation.AbstractValidator; | ||
import software.amazon.smithy.model.validation.ValidationEvent; | ||
import software.amazon.smithy.utils.Pair; | ||
|
||
public class LengthTraitValidator extends AbstractValidator { | ||
@Override | ||
public List<ValidationEvent> validate(Model model) { | ||
return model.shapes() | ||
.flatMap(shape -> Trait.flatMapStream(shape, LengthTrait.class)) | ||
.flatMap(pair -> validateLengthTrait(model, pair.getLeft(), pair.getRight()).stream()) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private List<ValidationEvent> validateLengthTrait(Model model, Shape shape, LengthTrait trait) { | ||
List<ValidationEvent> events = new ArrayList<>(); | ||
trait.getMin() | ||
.filter(min -> min < 0) | ||
.map(min -> error(shape, trait, "A length trait is applied with a negative `min` value.")) | ||
.ifPresent(events::add); | ||
|
||
trait.getMax() | ||
.filter(max -> max < 0) | ||
.map(max -> error(shape, trait, "A length trait is applied with a negative `max` value.")) | ||
.ifPresent(events::add); | ||
|
||
trait.getMin() | ||
.flatMap(min -> trait.getMax().map(max -> Pair.of(min, max))) | ||
.filter(pair -> pair.getLeft() > pair.getRight()) | ||
.map(pair -> error(shape, trait, "A length trait is applied with a `min` value greater than " | ||
+ "its `max` value.")) | ||
.map(events::add); | ||
return events; | ||
} | ||
} |
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
3 changes: 3 additions & 0 deletions
3
...src/test/resources/software/amazon/smithy/model/errorfiles/validators/length-trait.errors
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,3 @@ | ||
[ERROR] ns.foo#Invalid1: A length trait is applied with a negative `max` value. | LengthTrait | ||
[ERROR] ns.foo#Invalid2: A length trait is applied with a negative `min` value. | LengthTrait | ||
[ERROR] ns.foo#Invalid3: A length trait is applied with a `min` value greater than its `max` value. | LengthTrait |
73 changes: 73 additions & 0 deletions
73
...l/src/test/resources/software/amazon/smithy/model/errorfiles/validators/length-trait.json
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,73 @@ | ||
{ | ||
"smithy": "0.4.0", | ||
"ns.foo": { | ||
"shapes": { | ||
"Valid1": { | ||
"type": "list", | ||
"member": { | ||
"target": "smithy.api#String" | ||
}, | ||
"smithy.api#length": { | ||
"min": 0 | ||
} | ||
}, | ||
"Valid2": { | ||
"type": "list", | ||
"member": { | ||
"target": "smithy.api#String" | ||
}, | ||
"smithy.api#length": { | ||
"max": 1 | ||
} | ||
}, | ||
"Valid3": { | ||
"type": "list", | ||
"member": { | ||
"target": "smithy.api#String" | ||
}, | ||
"smithy.api#length": { | ||
"min": 0, | ||
"max": 1 | ||
} | ||
}, | ||
"Valid4": { | ||
"type": "list", | ||
"member": { | ||
"target": "smithy.api#String" | ||
}, | ||
"smithy.api#length": { | ||
"min": 4, | ||
"max": 4 | ||
} | ||
}, | ||
"Invalid1": { | ||
"type": "list", | ||
"member": { | ||
"target": "smithy.api#String" | ||
}, | ||
"smithy.api#length": { | ||
"max": -1 | ||
} | ||
}, | ||
"Invalid2": { | ||
"type": "list", | ||
"member": { | ||
"target": "smithy.api#String" | ||
}, | ||
"smithy.api#length": { | ||
"min": -1 | ||
} | ||
}, | ||
"Invalid3": { | ||
"type": "list", | ||
"member": { | ||
"target": "smithy.api#String" | ||
}, | ||
"smithy.api#length": { | ||
"min": 5, | ||
"max": 4 | ||
} | ||
} | ||
} | ||
} | ||
} |