-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathConfig.java
More file actions
169 lines (152 loc) · 6.01 KB
/
Config.java
File metadata and controls
169 lines (152 loc) · 6.01 KB
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
// Copyright 2023-2025 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.
package build.buf.protovalidate;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.TypeRegistry;
/** Config is the configuration for a Validator. */
public final class Config {
private static final TypeRegistry DEFAULT_TYPE_REGISTRY = TypeRegistry.getEmptyTypeRegistry();
private static final ExtensionRegistry DEFAULT_EXTENSION_REGISTRY =
ExtensionRegistry.getEmptyRegistry();
private final boolean failFast;
private final TypeRegistry typeRegistry;
private final ExtensionRegistry extensionRegistry;
private final boolean allowUnknownFields;
private Config(
boolean failFast,
TypeRegistry typeRegistry,
ExtensionRegistry extensionRegistry,
boolean allowUnknownFields) {
this.failFast = failFast;
this.typeRegistry = typeRegistry;
this.extensionRegistry = extensionRegistry;
this.allowUnknownFields = allowUnknownFields;
}
/**
* Create a new Configuration builder.
*
* @return a new Configuration builder.
*/
public static Builder newBuilder() {
return new Builder();
}
/**
* Checks if the configuration for failing fast is enabled.
*
* @return if failing fast is enabled
*/
public boolean isFailFast() {
return failFast;
}
/**
* Gets the type registry used for reparsing protobuf messages.
*
* @return a type registry
*/
public TypeRegistry getTypeRegistry() {
return typeRegistry;
}
/**
* Gets the extension registry used for resolving unknown protobuf extensions.
*
* @return an extension registry
*/
public ExtensionRegistry getExtensionRegistry() {
return extensionRegistry;
}
/**
* Checks if the configuration for allowing unknown rule fields is enabled.
*
* @return if allowing unknown rule fields is enabled
*/
public boolean isAllowingUnknownFields() {
return allowUnknownFields;
}
/** Builder for configuration. Provides a forward compatible API for users. */
public static final class Builder {
private boolean failFast;
private TypeRegistry typeRegistry = DEFAULT_TYPE_REGISTRY;
private ExtensionRegistry extensionRegistry = DEFAULT_EXTENSION_REGISTRY;
private boolean allowUnknownFields;
private Builder() {}
/**
* Set the configuration for failing fast.
*
* @param failFast the boolean for enabling
* @return this builder
*/
public Builder setFailFast(boolean failFast) {
this.failFast = failFast;
return this;
}
/**
* Set the type registry for reparsing protobuf messages. This option should be set alongside
* setExtensionRegistry to allow dynamic resolution of predefined rule extensions. It should be
* set to a TypeRegistry with all the message types from your file descriptor set registered. By
* default, if any unknown field rules are found, compilation of the rules will fail; use
* setAllowUnknownFields to control this behavior.
*
* <p>Note that the message types for any extensions in setExtensionRegistry must be present in
* the typeRegistry, and have an exactly-equal Descriptor. If the type registry is not set, the
* extension types in the extension registry must have exactly-equal Descriptor types to the
* protovalidate built-in messages. If these conditions are not met, extensions will not be
* resolved as expected. These conditions will be met when constructing a TypeRegistry and
* ExtensionRegistry using information from the same file descriptor sets.
*
* @param typeRegistry the type registry to use
* @return this builder
*/
public Builder setTypeRegistry(TypeRegistry typeRegistry) {
this.typeRegistry = typeRegistry;
return this;
}
/**
* Set the extension registry for resolving unknown extensions. This option should be set
* alongside setTypeRegistry to allow dynamic resolution of predefined rule extensions. It
* should be set to an ExtensionRegistry with all the extension types from your file descriptor
* set registered. By default, if any unknown field rules are found, compilation of the rules
* will fail; use setAllowUnknownFields to control this behavior.
*
* @param extensionRegistry the extension registry to use
* @return this builder
*/
public Builder setExtensionRegistry(ExtensionRegistry extensionRegistry) {
this.extensionRegistry = extensionRegistry;
return this;
}
/**
* Set whether unknown rule fields are allowed. If this setting is set to true, unknown standard
* predefined field rules and predefined field rule extensions will be ignored. This setting
* defaults to false, which will result in a CompilationException being thrown whenever an
* unknown field rule is encountered. Setting this to true will cause some field rules to be
* ignored; if the descriptor is dynamic, you can instead use setExtensionRegistry to provide
* dynamic type information that protovalidate can use to resolve the unknown fields.
*
* @param allowUnknownFields setting to apply
* @return this builder
*/
public Builder setAllowUnknownFields(boolean allowUnknownFields) {
this.allowUnknownFields = allowUnknownFields;
return this;
}
/**
* Build the corresponding {@link Config}.
*
* @return the configuration.
*/
public Config build() {
return new Config(failFast, typeRegistry, extensionRegistry, allowUnknownFields);
}
}
}