Skip to content

Update to protovalidate 0.10.3 #73

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 14 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ PROTOVALIDATE_VERSION ?= v$(shell <./deps/shared_deps.json jq -j .protovalidate.

# Set to use a different compiler. For example, `GO=go1.18rc1 make test`.
GO ?= go
ARGS ?= --strict
ARGS ?= --strict_message --expected_failures=buf/validate/conformance/expected_failures.yaml

BAZEL ?= bazel

.PHONY: help
Expand Down
1,483 changes: 1,483 additions & 0 deletions buf/validate/conformance/expected_failures.yaml

Large diffs are not rendered by default.

36 changes: 20 additions & 16 deletions buf/validate/internal/extra_func.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ bool IsHostname(std::string_view to_validate) {
to_validate = absl::StripSuffix(to_validate, ".");
std::vector<std::string_view> split = absl::StrSplit(to_validate, '.');
if (split.size() < 2) {
return re2::RE2::FullMatch(to_validate, component_regex)
&& !re2::RE2::FullMatch(to_validate, all_digits);
return re2::RE2::FullMatch(to_validate, component_regex) &&
!re2::RE2::FullMatch(to_validate, all_digits);
}
if (re2::RE2::FullMatch(split[split.size()-1], all_digits)) {
if (re2::RE2::FullMatch(split[split.size() - 1], all_digits)) {
return false;
}
for (size_t i = 0; i < split.size(); i++) {
Expand Down Expand Up @@ -203,14 +203,14 @@ cel::CelValue isEmail(google::protobuf::Arena* arena, cel::CelValue::StringHolde
bool IsIpv4(const std::string_view to_validate) {
// need to copy as string_view might not be null terminated
const auto str = std::string{to_validate};
struct in_addr result{};
struct in_addr result {};
return inet_pton(AF_INET, str.data(), &result) == 1;
}

bool IsIpv6(const std::string_view to_validate) {
// need to copy as string_view might not be null terminated
const auto str = std::string{to_validate};
struct in6_addr result{};
struct in6_addr result {};
return inet_pton(AF_INET6, str.data(), &result) == 1;
}

Expand Down Expand Up @@ -254,16 +254,15 @@ bool IsHostAndPort(const std::string_view str, const bool portRequired) {
const auto splitIdx = str.rfind(':');
if (str.front() == '[') {
const auto end = str.find(']');
const auto afterEnd = end+1;
const auto afterEnd = end + 1;
if (afterEnd == str.size()) { // no port
const auto host = str.substr(1, end-1);
const auto host = str.substr(1, end - 1);
return !portRequired && IsIpv6(host);
}
if (afterEnd == splitIdx) { // port
const auto host = str.substr(1, end-1);
const auto port = str.substr(splitIdx+1);
return IsIpv6(host)
&& IsPort(port);
const auto host = str.substr(1, end - 1);
const auto port = str.substr(splitIdx + 1);
return IsIpv6(host) && IsPort(port);
}
return false;
}
Expand All @@ -272,11 +271,12 @@ bool IsHostAndPort(const std::string_view str, const bool portRequired) {
return !portRequired && (IsHostname(str) || IsIpv4(str));
}
const auto host = str.substr(0, splitIdx);
const auto port = str.substr(splitIdx+1);
const auto port = str.substr(splitIdx + 1);
return (IsHostname(host) || IsIpv4(host)) && IsPort(port);
}

cel::CelValue isHostAndPort(google::protobuf::Arena* arena, cel::CelValue::StringHolder lhs, cel::CelValue rhs) {
cel::CelValue isHostAndPort(
google::protobuf::Arena* arena, cel::CelValue::StringHolder lhs, cel::CelValue rhs) {
const std::string_view str = lhs.value();
const bool portReq = rhs.BoolOrDie();
return cel::CelValue::CreateBool(IsHostAndPort(str, portReq));
Expand Down Expand Up @@ -434,7 +434,11 @@ cel::CelValue isUriRef(google::protobuf::Arena* arena, cel::CelValue::StringHold
scheme = split[0];
std::vector<std::string_view> hostSplit = absl::StrSplit(split[1], absl::MaxSplits('/', 1));
host = hostSplit[0];
remainder = hostSplit[1];
// If hostSplit has a size greater than 1, then a '/' appeared in the string. Set the rest
// to remainder so we can parse any query string.
if (hostSplit.size() > 1) {
remainder = hostSplit[1];
}
Comment on lines +437 to +441
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the segfault fix. The rest are just formatting changes.

}
std::vector<std::string_view> querySplit = absl::StrSplit(remainder, absl::MaxSplits('?', 1));
path = querySplit[0];
Expand Down Expand Up @@ -556,8 +560,8 @@ absl::Status RegisterExtraFuncs(
return isUriStatus;
}
auto isHostAndPortStatus =
cel::FunctionAdapter<cel::CelValue, cel::CelValue::StringHolder, cel::CelValue>::
CreateAndRegister("isHostAndPort", true, &isHostAndPort, &registry);
cel::FunctionAdapter<cel::CelValue, cel::CelValue::StringHolder, cel::CelValue>::
CreateAndRegister("isHostAndPort", true, &isHostAndPort, &registry);
if (!isHostAndPortStatus.ok()) {
return isHostAndPortStatus;
}
Expand Down
20 changes: 13 additions & 7 deletions buf/validate/validator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#include "eval/public/activation.h"
#include "eval/public/builtin_func_registrar.h"
#include "eval/public/cel_expr_builder_factory.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "parser/parser.h"

namespace buf::validate {
Expand Down Expand Up @@ -201,7 +201,8 @@ TEST(ValidatorTest, ValidateBadURIRefFailure) {
ASSERT_TRUE(violations_or.ok()) << violations_or.status();
EXPECT_EQ(violations_or.value().violations_size(), 1);
EXPECT_EQ(violations_or.value().violations(0).proto().constraint_id(), "string.uri_ref");
EXPECT_EQ(violations_or.value().violations(0).proto().message(), "value must be a valid URI");
EXPECT_EQ(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified my local clang-formatter is using the repo's clang-format rules.

violations_or.value().violations(0).proto().message(), "value must be a valid URI Reference");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the actual test fix. No idea how this was ever passing.

EXPECT_THAT(
violations_or.value().violations(0).field_value(),
Optional(FieldValueOf(VariantWith<std::string>("!@#$%^&*"))));
Expand Down Expand Up @@ -249,7 +250,8 @@ TEST(ValidatorTest, ValidateStringContainsFailure) {
EXPECT_EQ(violations_or.value().violations_size(), 1);
EXPECT_EQ(violations_or.value().violations(0).proto().constraint_id(), "string.contains");
EXPECT_EQ(
violations_or.value().violations(0).proto().message(), "value does not contain substring `bar`");
violations_or.value().violations(0).proto().message(),
"value does not contain substring `bar`");
}

TEST(ValidatorTest, ValidateStringContainsSuccess) {
Expand Down Expand Up @@ -305,7 +307,8 @@ TEST(ValidatorTest, ValidateStartsWithFailure) {
ASSERT_TRUE(violations_or.ok()) << violations_or.status();
EXPECT_EQ(violations_or.value().violations_size(), 1);
EXPECT_EQ(violations_or.value().violations(0).proto().constraint_id(), "string.prefix");
EXPECT_EQ(violations_or.value().violations(0).proto().message(), "value does not have prefix `foo`");
EXPECT_EQ(
violations_or.value().violations(0).proto().message(), "value does not have prefix `foo`");
}

TEST(ValidatorTest, ValidateStartsWithSuccess) {
Expand Down Expand Up @@ -333,7 +336,8 @@ TEST(ValidatorTest, ValidateEndsWithFailure) {
ASSERT_TRUE(violations_or.ok()) << violations_or.status();
EXPECT_EQ(violations_or.value().violations_size(), 1);
EXPECT_EQ(violations_or.value().violations(0).proto().constraint_id(), "string.suffix");
EXPECT_EQ(violations_or.value().violations(0).proto().message(), "value does not have suffix `baz`");
EXPECT_EQ(
violations_or.value().violations(0).proto().message(), "value does not have suffix `baz`");
}

TEST(ValidatorTest, ValidateHostnameSuccess) {
Expand Down Expand Up @@ -415,11 +419,13 @@ TEST(ValidatorTest, MessageConstraint) {
auto violations_or = validator.Validate(message_expressions);
ASSERT_TRUE(violations_or.ok()) << violations_or.status();
ASSERT_EQ(violations_or.value().violations_size(), 3);
EXPECT_EQ(violations_or.value().violations(0).proto().constraint_id(), "message_expression_scalar");
EXPECT_EQ(
violations_or.value().violations(0).proto().constraint_id(), "message_expression_scalar");
EXPECT_EQ(violations_or.value().violations(0).proto().message(), "a must be less than b");
EXPECT_EQ(violations_or.value().violations(1).proto().constraint_id(), "message_expression_enum");
EXPECT_EQ(violations_or.value().violations(1).proto().message(), "c must not equal d");
EXPECT_EQ(violations_or.value().violations(2).proto().constraint_id(), "message_expression_nested");
EXPECT_EQ(
violations_or.value().violations(2).proto().constraint_id(), "message_expression_nested");
EXPECT_EQ(violations_or.value().violations(2).proto().message(), "a must be greater than b");
}

Expand Down
14 changes: 14 additions & 0 deletions cmake/example/example.proto
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License 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.

syntax = "proto3";

package example.v1;
Expand Down
6 changes: 3 additions & 3 deletions deps/shared_deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
},
"protovalidate": {
"meta": {
"version": "0.10.0"
"version": "0.10.3"
},
"source": {
"sha256": "5879da3a72bcbd48e42f84b1eb0dfb45e3657fb68232b6b7c12c01bb937b659d",
"sha256": "12c6a8018ea57a941ede58a9273f266e180b1f0800d963527de26bb26c876004",
"strip_prefix": "protovalidate-{version}",
"urls": [
"https://github.com/bufbuild/protovalidate/archive/v{version}.tar.gz"
"https://github.com/bufbuild/protovalidate/releases/download/v{version}/protovalidate-{version}.tar.gz"
]
}
}
Expand Down
Loading