Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ public List<RuleViolation.Builder> evaluate(Value val, boolean failFast) {
}
}

if (pattern != null && !pattern.matches(strVal)) {
if (pattern != null && !pattern.matcher(strVal).find()) {
violations =
RuleBase.add(
violations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.example.noimports.validationtest.ExampleStringEmail;
import com.example.noimports.validationtest.ExampleStringHostAndPort;
import com.example.noimports.validationtest.ExampleStringMinMaxLen;
import com.example.noimports.validationtest.ExampleStringPattern;
import com.example.noimports.validationtest.HttpHeaderName;
import com.google.protobuf.Descriptors.FieldDescriptor;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -122,6 +123,24 @@ void hostAndPortAcceptsValidAndRejectsInvalid() throws ValidationException {
.isFalse();
}

@ParameterizedTest
@ValueSource(strings = {"a", "ab", "battery-1", "x y"})
void stringPatternAcceptsSubstringMatch(String input) throws ValidationException {
Validator v = nativeValidator();
assertThat(v.validate(ExampleStringPattern.newBuilder().setVal(input).build()).isSuccess())
.isTrue();
}

@ParameterizedTest
@ValueSource(strings = {" ", ""})
void stringPatternRejectsNonMatching(String input) throws ValidationException {
Validator v = nativeValidator();
ValidationResult result = v.validate(ExampleStringPattern.newBuilder().setVal(input).build());
assertThat(result.isSuccess()).isFalse();
assertThat(result.getViolations()).hasSize(1);
assertThat(result.getViolations().get(0).toProto().getRuleId()).isEqualTo("string.pattern");
}

@Test
void nativeAndCelProduceEqualViolationProto() throws ValidationException {
ExampleStringConst msg = ExampleStringConst.newBuilder().setVal("nope").build();
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/proto/validationtest/validationtest.proto
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ message ExampleStringMinMaxLen {
}];
}

message ExampleStringPattern {
string val = 1 [(buf.validate.field).string.pattern = "\\S"];
}

message ExampleStringHostAndPort {
string val = 1 [(buf.validate.field).string.host_and_port = true];
}
Expand Down