forked from graphql-java/graphql-java-extended-validation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidationEnvironment.java
264 lines (224 loc) · 8.91 KB
/
ValidationEnvironment.java
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package graphql.validation.rules;
import graphql.PublicApi;
import graphql.execution.ResultPath;
import graphql.language.SourceLocation;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLAppliedDirective;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLFieldsContainer;
import graphql.schema.GraphQLInputType;
import graphql.validation.interpolation.MessageInterpolator;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Consumer;
/**
* The environment in which validation runs
*/
@PublicApi
public class ValidationEnvironment {
/**
* The type of element being validated
*/
public enum ValidatedElement {
/**
* A output field is being validated
*/
FIELD,
/**
* An argument on a graphql output field is being validated
*/
ARGUMENT,
/**
* A input type field is being validated
*/
INPUT_OBJECT_FIELD
}
private final GraphQLFieldsContainer fieldsContainer;
private final GraphQLFieldDefinition fieldDefinition;
private final GraphQLArgument argument;
private final ResultPath executionPath;
private final ResultPath validatedPath;
private final SourceLocation location;
private final MessageInterpolator interpolator;
private final Map<Class, Object> contextMap;
private final Locale locale;
private final Map<String, Object> argumentValues;
private final Object validatedValue;
private final GraphQLInputType validatedType;
private final ValidatedElement validatedElement;
private final List<GraphQLAppliedDirective> directives;
private ValidationEnvironment(Builder builder) {
this.argument = builder.argument;
this.argumentValues = Collections.unmodifiableMap(builder.argumentValues);
this.contextMap = Collections.unmodifiableMap(builder.contextMap);
this.fieldDefinition = builder.fieldDefinition;
this.executionPath = builder.executionPath;
this.validatedPath = builder.validatedPath;
this.validatedType = builder.validatedType;
this.fieldsContainer = builder.fieldsContainer;
this.interpolator = builder.interpolator;
this.locale = builder.locale;
this.location = builder.location;
this.validatedValue = builder.validatedValue;
this.validatedElement = builder.validatedElement;
this.directives = builder.directives;
}
public static Builder newValidationEnvironment() {
return new Builder();
}
@SuppressWarnings("unchecked")
public <T> T getContextObject(Class<T> clazz, Object... defaultVal) {
return (T) contextMap.getOrDefault(clazz, defaultVal.length == 0 ? null : defaultVal[0]);
}
public GraphQLFieldsContainer getFieldsContainer() {
return fieldsContainer;
}
public GraphQLFieldDefinition getFieldDefinition() {
return fieldDefinition;
}
public GraphQLArgument getArgument() {
return argument;
}
public SourceLocation getLocation() {
return location;
}
public ResultPath getValidatedPath() {
return validatedPath;
}
public ResultPath getExecutionPath() {
return executionPath;
}
public GraphQLInputType getValidatedType() {
return validatedType;
}
public Object getValidatedValue() {
return validatedValue;
}
public Map<String, Object> getArgumentValues() {
return argumentValues;
}
public MessageInterpolator getInterpolator() {
return interpolator;
}
public Locale getLocale() {
return locale;
}
public ValidatedElement getValidatedElement() {
return validatedElement;
}
public List<GraphQLAppliedDirective> getDirectives() {
return directives;
}
public ValidationEnvironment transform(Consumer<Builder> builderConsumer) {
Builder builder = newValidationEnvironment().validationEnvironment(this);
builderConsumer.accept(builder);
return builder.build();
}
public static class Builder {
private final Map<Class, Object> contextMap = new HashMap<>();
private GraphQLArgument argument;
private Map<String, Object> argumentValues = new HashMap<>();
private GraphQLFieldDefinition fieldDefinition;
private ResultPath validatedPath = ResultPath.rootPath();
private ResultPath executionPath;
private GraphQLFieldsContainer fieldsContainer;
private MessageInterpolator interpolator;
private Locale locale;
private SourceLocation location;
private Object validatedValue;
private GraphQLInputType validatedType;
private ValidatedElement validatedElement;
private List<GraphQLAppliedDirective> directives = Collections.emptyList();
public Builder validationEnvironment(ValidationEnvironment validationEnvironment) {
this.argument = validationEnvironment.argument;
this.argumentValues = validationEnvironment.argumentValues;
this.contextMap.putAll(validationEnvironment.contextMap);
this.fieldDefinition = validationEnvironment.fieldDefinition;
this.executionPath = validationEnvironment.executionPath;
this.validatedPath = validationEnvironment.validatedPath;
this.validatedType = validationEnvironment.validatedType;
this.fieldsContainer = validationEnvironment.fieldsContainer;
this.interpolator = validationEnvironment.interpolator;
this.locale = validationEnvironment.locale;
this.location = validationEnvironment.location;
this.validatedValue = validationEnvironment.validatedValue;
this.validatedElement = validationEnvironment.validatedElement;
this.directives = validationEnvironment.directives;
return this;
}
public Builder dataFetchingEnvironment(DataFetchingEnvironment dataFetchingEnvironment) {
fieldsContainer(dataFetchingEnvironment.getExecutionStepInfo().getObjectType());
fieldDefinition(dataFetchingEnvironment.getFieldDefinition());
directives(dataFetchingEnvironment.getFieldDefinition().getAppliedDirectives());
executionPath(dataFetchingEnvironment.getExecutionStepInfo().getPath());
validatedPath(dataFetchingEnvironment.getExecutionStepInfo().getPath());
location(dataFetchingEnvironment.getField().getSourceLocation());
argumentValues(dataFetchingEnvironment.getArguments());
validatedElement(ValidatedElement.FIELD);
return this;
}
public Builder argument(GraphQLArgument argument) {
this.argument = argument;
return this;
}
public Builder context(Class clazz, Object value) {
this.contextMap.put(clazz, value);
return this;
}
public Builder fieldsContainer(GraphQLFieldsContainer fieldsContainer) {
this.fieldsContainer = fieldsContainer;
return this;
}
public Builder executionPath(ResultPath executionPath) {
this.executionPath = executionPath;
return this;
}
public Builder fieldDefinition(GraphQLFieldDefinition fieldDefinition) {
this.fieldDefinition = fieldDefinition;
return this;
}
public Builder validatedElement(ValidatedElement validatedElement) {
this.validatedElement = validatedElement;
return this;
}
public Builder validatedType(GraphQLInputType validatedType) {
this.validatedType = validatedType;
return this;
}
public Builder validatedValue(Object validatedValue) {
this.validatedValue = validatedValue;
return this;
}
public Builder validatedPath(ResultPath validatedPath) {
this.validatedPath = validatedPath;
return this;
}
public Builder argumentValues(Map<String, Object> argumentValues) {
this.argumentValues = argumentValues;
return this;
}
public Builder location(SourceLocation location) {
this.location = location;
return this;
}
public Builder messageInterpolator(MessageInterpolator interpolator) {
this.interpolator = interpolator;
return this;
}
public Builder locale(Locale locale) {
this.locale = locale;
return this;
}
public Builder directives(List<GraphQLAppliedDirective> directives) {
this.directives = directives;
return this;
}
public ValidationEnvironment build() {
return new ValidationEnvironment(this);
}
}
}