-
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.
Add warning for pattern traits missing ^ or $ anchors
The pattern trait does not implicitly add a leading ^ or trailing $ to match an entire string. If customer thinks the anchors are implicitly added they may allow more values than expected. This WARNING will help customers identify if they unintentionally forgot the anchors.
- Loading branch information
Showing
8 changed files
with
134 additions
and
7 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
62 changes: 62 additions & 0 deletions
62
...c/main/java/software/amazon/smithy/model/validation/validators/PatternTraitValidator.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,62 @@ | ||
/* | ||
* Copyright 2022 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.StringJoiner; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
import software.amazon.smithy.model.traits.PatternTrait; | ||
import software.amazon.smithy.model.validation.AbstractValidator; | ||
import software.amazon.smithy.model.validation.ValidationEvent; | ||
|
||
/** | ||
* Emits a validation event if a pattern trait is not anchored. | ||
*/ | ||
public final class PatternTraitValidator extends AbstractValidator { | ||
|
||
@Override | ||
public List<ValidationEvent> validate(Model model) { | ||
List<ValidationEvent> events = new ArrayList<>(); | ||
for (Shape shape : model.getShapesWithTrait(PatternTrait.class)) { | ||
validatePatternTrait(events, shape); | ||
} | ||
|
||
return events; | ||
} | ||
|
||
private void validatePatternTrait(List<ValidationEvent> events, Shape shape) { | ||
PatternTrait trait = shape.expectTrait(PatternTrait.class); | ||
String pattern = trait.getValue(); | ||
boolean leading = pattern.startsWith("^"); | ||
boolean trailing = pattern.endsWith("$"); | ||
if (!leading || !trailing) { | ||
StringJoiner sj = new StringJoiner(" and "); | ||
if (!leading) { | ||
sj.add("leading '^'"); | ||
} | ||
if (!trailing) { | ||
sj.add("trailing '$'"); | ||
} | ||
events.add(warning(shape, trait, String.format( | ||
"A pattern trait is applied without a %s, meaning only part of the string must match the regular " | ||
+ "expression. Explicitly anchoring regular expressions is preferable because it is more " | ||
+ "restrictive by default and does not require modelers to understand that Smithy patterns are " | ||
+ "not automatically anchored.", sj))); | ||
} | ||
} | ||
} |
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
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
6 changes: 6 additions & 0 deletions
6
...rc/test/resources/software/amazon/smithy/model/errorfiles/validators/pattern-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,6 @@ | ||
[WARNING] ns.foo#Structure$WithoutAnchorsMember: A pattern trait is applied without a leading '^' and trailing '$', meaning only part of the string must match the regular expression. Explicitly anchoring regular expressions is preferable | PatternTrait | ||
[WARNING] ns.foo#Structure$WithoutLeadingAnchorMember: A pattern trait is applied without a leading '^', meaning only part of the string must match the regular expression. Explicitly anchoring regular expressions is preferable | PatternTrait | ||
[WARNING] ns.foo#Structure$WithoutTrailingAnchorMember: A pattern trait is applied without a trailing '$', meaning only part of the string must match the regular expression. Explicitly anchoring regular expressions is preferable | PatternTrait | ||
[WARNING] ns.foo#WithoutAnchors: A pattern trait is applied without a leading '^' and trailing '$', meaning only part of the string must match the regular expression. Explicitly anchoring regular expressions is preferable | PatternTrait | ||
[WARNING] ns.foo#WithoutLeadingAnchor: A pattern trait is applied without a leading '^', meaning only part of the string must match the regular expression. Explicitly anchoring regular expressions is preferable | PatternTrait | ||
[WARNING] ns.foo#WithoutTrailingAnchor: A pattern trait is applied without a trailing '$', meaning only part of the string must match the regular expression. Explicitly anchoring regular expressions is preferable | PatternTrait |
58 changes: 58 additions & 0 deletions
58
.../src/test/resources/software/amazon/smithy/model/errorfiles/validators/pattern-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,58 @@ | ||
{ | ||
"smithy": "1.0", | ||
"shapes": { | ||
"ns.foo#Structure": { | ||
"type": "structure", | ||
"members": { | ||
"AnchoredMember": { | ||
"target": "smithy.api#String", | ||
"traits": { | ||
"smithy.api#pattern": "^[a-z]$" | ||
} | ||
}, | ||
"WithoutAnchorsMember": { | ||
"target": "smithy.api#String", | ||
"traits": { | ||
"smithy.api#pattern": "[a-z]" | ||
} | ||
}, | ||
"WithoutLeadingAnchorMember": { | ||
"target": "smithy.api#String", | ||
"traits": { | ||
"smithy.api#pattern": "[a-z]$" | ||
} | ||
}, | ||
"WithoutTrailingAnchorMember": { | ||
"target": "smithy.api#String", | ||
"traits": { | ||
"smithy.api#pattern": "^[a-z]" | ||
} | ||
} | ||
} | ||
}, | ||
"ns.foo#Anchored": { | ||
"type": "string", | ||
"traits": { | ||
"smithy.api#pattern": "^[a-z]$" | ||
} | ||
}, | ||
"ns.foo#WithoutAnchors": { | ||
"type": "string", | ||
"traits": { | ||
"smithy.api#pattern": "[a-z]" | ||
} | ||
}, | ||
"ns.foo#WithoutLeadingAnchor": { | ||
"type": "string", | ||
"traits": { | ||
"smithy.api#pattern": "[a-z]$" | ||
} | ||
}, | ||
"ns.foo#WithoutTrailingAnchor": { | ||
"type": "string", | ||
"traits": { | ||
"smithy.api#pattern": "^[a-z]" | ||
} | ||
} | ||
} | ||
} |
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