Skip to content

add new validation type not_contains #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ vendor:
lint:
# lints the package for common code smells
which golint || go get -u golang.org/x/lint/golint
which shadow || go get -u golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow
test -z "$(gofmt -d -s ./*.go)" || (gofmt -d -s ./*.go && exit 1)
# golint -set_exit_status
go tool vet -all -shadow -shadowstrict *.go
# check for variable shadowing
go vet -vettool=$(which shadow) *.go

gogofast:
go build -o $@ vendor/github.com/gogo/protobuf/protoc-gen-gogofast/main.go
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ Check the [constraint rule comparison matrix](rule_comparison.md) for language-s
string x = 1 [(validate.rules).string.pattern = "(?i)^[0-9a-f]+$"];
```

- **prefix/suffix/contains**: the field must contain the specified substring in an optionally explicit location.
- **prefix/suffix/contains/not_contains**: the field must contain the specified substring in an optionally explicit location, or not contain the specified substring.

```protobuf
// x must begin with "foo"
Expand All @@ -346,6 +346,9 @@ Check the [constraint rule comparison matrix](rule_comparison.md) for language-s

// x must contain "baz" anywhere inside it
string x = 1 [(validate.rules).string.contains = "baz"];

// x cannot contain "baz" anywhere inside it
string x = 1 [(validate.rules).string.not_contains = "baz"];

// x must begin with "fizz" and end with "buzz"
string x = 1 [(validate.rules).string = {prefix: "fizz", suffix: "buzz"}];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ public static void contains(String field, String value, String contains) throws
}
}

public static void notContains(String field, String value, String contains) throws ValidationException {
if (value.contains(contains)) {
throw new ValidationException(field, enquote(value), "should not contain " + contains);
}
}


public static void suffix(String field, String value, String suffix) throws ValidationException {
if (!value.endsWith(suffix)) {
throw new ValidationException(field, enquote(value), "should end with " + suffix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ public void containsWorks() throws ValidationException {
assertThatThrownBy(() -> StringValidation.contains("x", "Hello World", "Bananas")).isInstanceOf(ValidationException.class);
}

@Test
public void notContainsWorks() throws ValidationException {
// Match
StringValidation.notContains("x", "Hello World", "Bananas");
// No Match
assertThatThrownBy(() -> StringValidation.notContains("x", "Hello World", "o W")).isInstanceOf(ValidationException.class);
}

@Test
public void suffixWorks() throws ValidationException {
// Match
Expand Down
1 change: 1 addition & 0 deletions rule_comparison.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
| min\_bytes/max\_bytes |✅|✅|✅|✅|✅|
| pattern |✅|✅|❌|✅|✅|
| prefix/suffix/contains |✅|✅|✅|✅|✅|
| contains/not_contains |✅|✅|✅|✅|✅|
| in/not_in |✅|✅|✅|✅|✅|
| email |✅|✅|❌|✅|✅|
| hostname |✅|✅|✅|✅|✅|
Expand Down
8 changes: 8 additions & 0 deletions templates/cc/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ const strTpl = `
{{ err . "value does not contain substring " (lit $r.GetContains) }}
}
}
{{ end }}

{{ if $r.NotContains }}
{
if (pgv::Contains({{ accessor . }}, {{ lit $r.GetNotContains }})) {
{{ err . "value contains substring " (lit $r.GetNotContains) }}
}
}
{{ end }}

{{ if $r.GetIp }}
Expand Down
6 changes: 6 additions & 0 deletions templates/goshared/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ const strTpl = `
}
{{ end }}

{{ if $r.NotContains }}
if strings.Contains({{ accessor . }}, {{ lit $r.GetNotContains }}) {
return {{ err . "value contains substring " (lit $r.GetNotContains) }}
}
{{ end }}

{{ if $r.GetIp }}
if ip := net.ParseIP({{ accessor . }}); ip == nil {
return {{ err . "value must be a valid IP address" }}
Expand Down
3 changes: 3 additions & 0 deletions templates/java/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ const stringTpl = `{{ $f := .Field }}{{ $r := .Rules -}}
{{- if $r.Contains }}
io.envoyproxy.pgv.StringValidation.contains("{{ $f.FullyQualifiedName }}", {{ accessor . }}, "{{ $r.GetContains }}");
{{- end -}}
{{- if $r.NotContains }}
io.envoyproxy.pgv.StringValidation.notContains("{{ $f.FullyQualifiedName }}", {{ accessor . }}, "{{ $r.GetNotContains }}");
{{- end -}}
{{- if $r.Suffix }}
io.envoyproxy.pgv.StringValidation.suffix("{{ $f.FullyQualifiedName }}", {{ accessor . }}, "{{ $r.GetSuffix }}");
{{- end -}}
Expand Down
1 change: 1 addition & 0 deletions tests/harness/cases/strings.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ message StringPattern { string val = 1 [(validate.rules).string.pattern =
message StringPatternEscapes { string val = 1 [(validate.rules).string.pattern = "\\* \\\\ \\w"]; }
message StringPrefix { string val = 1 [(validate.rules).string.prefix = "foo"]; }
message StringContains { string val = 1 [(validate.rules).string.contains = "bar"]; }
message StringNotContains { string val = 1 [(validate.rules).string.not_contains = "bar"]; }
message StringSuffix { string val = 1 [(validate.rules).string.suffix = "baz"]; }
message StringEmail { string val = 1 [(validate.rules).string.email = true]; }
message StringAddress { string val = 1 [(validate.rules).string.address = true]; }
Expand Down
5 changes: 5 additions & 0 deletions tests/harness/executor/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,11 @@ var stringCases = []TestCase{
{"string - contains - invalid", &cases.StringContains{Val: "candy bazs"}, false},
{"string - contains - invalid (case-sensitive)", &cases.StringContains{Val: "Candy Bars"}, false},

{"string - not contains - valid", &cases.StringNotContains{Val: "candy bazs"}, true},
{"string - not contains - valid (case-sensitive)", &cases.StringNotContains{Val: "Candy Bars"}, true},
{"string - not contains - invalid", &cases.StringNotContains{Val: "candy bars"}, false},
{"string - not contains - invalid (equal)", &cases.StringNotContains{Val: "bar"}, false},

{"string - suffix - valid", &cases.StringSuffix{Val: "foobaz"}, true},
{"string - suffix - valid (only)", &cases.StringSuffix{Val: "baz"}, true},
{"string - suffix - invalid", &cases.StringSuffix{Val: "foobar"}, false},
Expand Down
5 changes: 5 additions & 0 deletions validate/validate.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ static inline bool Contains(const string& search_in, const string& to_find)
return search_in.find(to_find) != string::npos;
}

static inline bool NotContains(const string& search_in, const string& to_find)
{
return not Contains(search_in, to_find);
}

static inline bool IsIpv4(const string& to_validate) {
struct sockaddr_in sa;
return !(inet_pton(AF_INET, to_validate.c_str(), &sa.sin_addr) < 1);
Expand Down
Loading