| 1 |
package v4 |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/aws/aws-sdk-go/internal/strings" |
| 5 |
) |
| 6 |
|
| 7 |
// validator houses a set of rule needed for validation of a |
| 8 |
// string value |
| 9 |
type rules []rule |
| 10 |
|
| 11 |
// rule interface allows for more flexible rules and just simply |
| 12 |
// checks whether or not a value adheres to that rule |
| 13 |
type rule interface { |
| 14 |
IsValid(value string) bool |
| 15 |
} |
| 16 |
|
| 17 |
// IsValid will iterate through all rules and see if any rules |
| 18 |
// apply to the value and supports nested rules |
| 19 |
func (r rules) IsValid(value string) bool { |
| 20 |
for _, rule := range r { |
| 21 |
if rule.IsValid(value) { |
| 22 |
return true |
| 23 |
} |
| 24 |
} |
| 25 |
return false |
| 26 |
} |
| 27 |
|
| 28 |
// mapRule generic rule for maps |
| 29 |
type mapRule map[string]struct{} |
| 30 |
|
| 31 |
// IsValid for the map rule satisfies whether it exists in the map |
| 32 |
func (m mapRule) IsValid(value string) bool { |
| 33 |
_, ok := m[value] |
| 34 |
return ok |
| 35 |
} |
| 36 |
|
| 37 |
// allowList is a generic rule for allow listing |
| 38 |
type allowList struct { |
| 39 |
rule |
| 40 |
} |
| 41 |
|
| 42 |
// IsValid for allow list checks if the value is within the allow list |
| 43 |
func (w allowList) IsValid(value string) bool { |
| 44 |
return w.rule.IsValid(value) |
| 45 |
} |
| 46 |
|
| 47 |
// excludeList is a generic rule for exclude listing |
| 48 |
type excludeList struct { |
| 49 |
rule |
| 50 |
} |
| 51 |
|
| 52 |
// IsValid for exclude list checks if the value is within the exclude list |
| 53 |
func (b excludeList) IsValid(value string) bool { |
| 54 |
return !b.rule.IsValid(value) |
| 55 |
} |
| 56 |
|
| 57 |
type patterns []string |
| 58 |
|
| 59 |
// IsValid for patterns checks each pattern and returns if a match has |
| 60 |
// been found |
| 61 |
func (p patterns) IsValid(value string) bool { |
| 62 |
for _, pattern := range p { |
| 63 |
if strings.HasPrefixFold(value, pattern) { |
| 64 |
return true |
| 65 |
} |
| 66 |
} |
| 67 |
return false |
| 68 |
} |
| 69 |
|
| 70 |
// inclusiveRules rules allow for rules to depend on one another |
| 71 |
type inclusiveRules []rule |
| 72 |
|
| 73 |
// IsValid will return true if all rules are true |
| 74 |
func (r inclusiveRules) IsValid(value string) bool { |
| 75 |
for _, rule := range r { |
| 76 |
if !rule.IsValid(value) { |
| 77 |
return false |
| 78 |
} |
| 79 |
} |
| 80 |
return true |
| 81 |
} |