-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvalidator.cc
192 lines (182 loc) · 6.83 KB
/
validator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// 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.
#include "buf/validate/validator.h"
namespace buf::validate {
absl::Status Validator::ValidateMessage(
internal::ConstraintContext& ctx, const google::protobuf::Message& message) {
const auto* constraints_or = factory_->GetMessageConstraints(message.GetDescriptor());
if (constraints_or == nullptr) {
return absl::NotFoundError(
"constraints not loaded for message: " + message.GetDescriptor()->full_name());
}
if (!constraints_or->ok()) {
return constraints_or->status();
}
for (const auto& constraint : constraints_or->value()) {
auto status = constraint->Validate(ctx, message);
if (ctx.shouldReturn(status)) {
return status;
}
}
return ValidateFields(ctx, message);
}
absl::Status Validator::ValidateFields(
internal::ConstraintContext& ctx, const google::protobuf::Message& message) {
// Validate any set fields that are messages.
std::vector<const google::protobuf::FieldDescriptor*> fields;
message.GetReflection()->ListFields(message, &fields);
for (const auto& field : fields) {
if (field->cpp_type() != google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
continue;
}
if (field->options().HasExtension(validate::field)) {
const auto& fieldExt = field->options().GetExtension(validate::field);
if (fieldExt.ignore() == IGNORE_ALWAYS || fieldExt.skipped() ||
(fieldExt.has_repeated() &&
(fieldExt.repeated().items().ignore() == IGNORE_ALWAYS ||
fieldExt.repeated().items().skipped())) ||
(fieldExt.has_map() &&
(fieldExt.map().values().ignore() == IGNORE_ALWAYS ||
fieldExt.map().values().skipped()))) {
continue;
}
}
if (field->is_map()) {
const auto* mapEntryDesc = field->message_type();
const auto* keyField = mapEntryDesc->FindFieldByName("key");
const auto* valueField = mapEntryDesc->FindFieldByName("value");
if (keyField == nullptr || valueField == nullptr) {
return absl::InternalError("map entry missing key or value field");
}
if (valueField->cpp_type() != google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
continue;
}
int size = message.GetReflection()->FieldSize(message, field);
for (int i = 0; i < size; i++) {
const auto& elemMsg = message.GetReflection()->GetRepeatedMessage(message, field, i);
const auto& valueMsg = elemMsg.GetReflection()->GetMessage(elemMsg, valueField);
int pos = ctx.violations.size();
auto status = ValidateMessage(ctx, valueMsg);
if (pos < ctx.violations.size()) {
FieldPathElement element = internal::fieldPathElement(field);
if (auto status = internal::setPathElementMapKey(&element, elemMsg, keyField, valueField);
!status.ok()) {
return status;
}
ctx.appendFieldPathElement(element, pos);
}
if (ctx.shouldReturn(status)) {
return status;
}
}
} else if (field->is_repeated()) {
int size = message.GetReflection()->FieldSize(message, field);
for (int i = 0; i < size; i++) {
int pos = ctx.violations.size();
const auto& subMsg = message.GetReflection()->GetRepeatedMessage(message, field, i);
auto status = ValidateMessage(ctx, subMsg);
if (pos < ctx.violations.size()) {
FieldPathElement element = internal::fieldPathElement(field);
element.set_index(i);
ctx.appendFieldPathElement(element, pos);
}
if (ctx.shouldReturn(status)) {
return status;
}
}
} else {
const auto& subMsg = message.GetReflection()->GetMessage(message, field);
int pos = ctx.violations.size();
auto status = ValidateMessage(ctx, subMsg);
if (pos < ctx.violations.size()) {
ctx.appendFieldPathElement(internal::fieldPathElement(field), pos);
}
if (ctx.shouldReturn(status)) {
return status;
}
}
}
return absl::OkStatus();
}
absl::StatusOr<ValidationResult> Validator::Validate(const google::protobuf::Message& message) {
internal::ConstraintContext ctx;
ctx.failFast = failFast_;
ctx.arena = arena_;
auto status = ValidateMessage(ctx, message);
if (!status.ok()) {
return status;
}
ctx.finalize();
return ValidationResult{std::move(ctx.violations)};
}
absl::StatusOr<std::unique_ptr<ValidatorFactory>> ValidatorFactory::New() {
std::unique_ptr<ValidatorFactory> result(new ValidatorFactory());
auto builder_or = internal::NewConstraintBuilder(&result->arena_);
if (!builder_or.ok()) {
return builder_or.status();
}
absl::MutexLock lock(&result->mutex_);
result->builder_ = std::move(builder_or).value();
return result;
}
absl::Status ValidatorFactory::Add(const google::protobuf::Descriptor* desc) {
{
absl::WriterMutexLock lock(&mutex_);
auto iter = constraints_.find(desc);
if (iter != constraints_.end()) {
return iter->second.status();
}
auto status =
constraints_
.emplace(
desc, internal::NewMessageConstraints(messageFactory_, allowUnknownFields_, &arena_, *builder_, desc))
.first->second.status();
if (!status.ok()) {
return status;
}
}
// Add all message fields recursively.
for (int i = 0; i < desc->field_count(); i++) {
const google::protobuf::FieldDescriptor* field = desc->field(i);
if (field->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
if (auto status = Add(field->message_type()); !status.ok()) {
return status;
}
}
}
return absl::OkStatus();
}
const internal::Constraints* ValidatorFactory::GetMessageConstraints(
const google::protobuf::Descriptor* desc) {
{
absl::ReaderMutexLock lock(&mutex_);
auto iter = constraints_.find(desc);
if (iter != constraints_.end()) {
return &iter->second;
}
if (disableLazyLoading_) {
return nullptr;
}
}
absl::WriterMutexLock lock(&mutex_);
auto iter = constraints_.find(desc);
if (iter != constraints_.end()) {
return &iter->second;
}
return &constraints_
.emplace(
desc, internal::NewMessageConstraints(messageFactory_, allowUnknownFields_, &arena_, *builder_, desc))
.first->second;
}
} // namespace buf::validate