forked from graphql-java/graphql-java-extended-validation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationRules.java
More file actions
181 lines (147 loc) · 6.74 KB
/
ValidationRules.java
File metadata and controls
181 lines (147 loc) · 6.74 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
170
171
172
173
174
175
176
177
178
179
180
181
package graphql.validation.rules;
import graphql.GraphQLError;
import graphql.PublicApi;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLFieldsContainer;
import graphql.validation.constraints.DirectiveConstraints;
import graphql.validation.interpolation.MessageInterpolator;
import graphql.validation.interpolation.ResourceBundleMessageInterpolator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import static graphql.Assert.assertNotNull;
/**
* {@link ValidationRules} is a holder of validation rules
* and you can then pass it field and arguments and narrow down the list of actual rules
* that apply to those fields and arguments.
* <p>
* It also allows you to run the appropriate rules via the
* {@link #runValidationRules(graphql.schema.DataFetchingEnvironment)} method.
*/
@PublicApi
public class ValidationRules {
private final OnValidationErrorStrategy onValidationErrorStrategy;
private final List<ValidationRule> rules;
private final MessageInterpolator messageInterpolator;
private final Locale locale;
private ValidationRules(Builder builder) {
this.rules = Collections.unmodifiableList(builder.rules);
this.messageInterpolator = builder.messageInterpolator;
this.onValidationErrorStrategy = builder.onValidationErrorStrategy;
this.locale = builder.locale;
}
public MessageInterpolator getMessageInterpolator() {
return messageInterpolator;
}
public Locale getLocale() {
return locale;
}
public List<ValidationRule> getRules() {
return rules;
}
public OnValidationErrorStrategy getOnValidationErrorStrategy() {
return onValidationErrorStrategy;
}
public TargetedValidationRules buildRulesFor(GraphQLFieldDefinition fieldDefinition, GraphQLFieldsContainer fieldsContainer) {
TargetedValidationRules.Builder rulesBuilder = TargetedValidationRules.newValidationRules();
ValidationCoordinates fieldCoordinates = ValidationCoordinates.newCoordinates(fieldsContainer, fieldDefinition);
List<ValidationRule> fieldRules = getRulesFor(fieldDefinition, fieldsContainer);
rulesBuilder.addRules(fieldCoordinates, fieldRules);
for (GraphQLArgument fieldArg : fieldDefinition.getArguments()) {
ValidationCoordinates validationCoordinates = ValidationCoordinates.newCoordinates(fieldsContainer, fieldDefinition, fieldArg);
List<ValidationRule> rules = getRulesFor(fieldArg, fieldDefinition, fieldsContainer);
rulesBuilder.addRules(validationCoordinates, rules);
}
return rulesBuilder.build();
}
public List<ValidationRule> getRulesFor(GraphQLArgument fieldArg, GraphQLFieldDefinition fieldDefinition, GraphQLFieldsContainer fieldsContainer) {
return rules.stream()
.filter(rule -> rule.appliesTo(fieldArg, fieldDefinition, fieldsContainer))
.collect(Collectors.toList());
}
public List<ValidationRule> getRulesFor(GraphQLFieldDefinition fieldDefinition, GraphQLFieldsContainer fieldsContainer) {
return rules.stream()
.filter(rule -> rule.appliesTo(fieldDefinition, fieldsContainer))
.collect(Collectors.toList());
}
/**
* This helper method will run the validation rules that apply to the provided {@link graphql.schema.DataFetchingEnvironment}
*
* @param env the data fetching environment
*
* @return a list of zero or more input data validation errors
*/
public List<GraphQLError> runValidationRules(DataFetchingEnvironment env) {
GraphQLFieldsContainer fieldsContainer = env.getExecutionStepInfo().getObjectType();
GraphQLFieldDefinition fieldDefinition = env.getFieldDefinition();
MessageInterpolator messageInterpolator = this.getMessageInterpolator();
TargetedValidationRules rules = this.buildRulesFor(fieldDefinition, fieldsContainer);
return rules.runValidationRules(env, messageInterpolator, this.getLocale());
}
/**
* A builder of validation rules. By default the SDL @directive rules from
* {@link graphql.validation.constraints.DirectiveConstraints#STANDARD_CONSTRAINTS} are included
* but you can add extra rules or call {@link graphql.validation.rules.ValidationRules.Builder#clearRules()}
* to start afresh.
*
* @return a new builder of rules
*/
public static Builder newValidationRules() {
return new Builder();
}
public static class Builder {
private Locale locale;
private OnValidationErrorStrategy onValidationErrorStrategy = OnValidationErrorStrategy.RETURN_NULL;
private MessageInterpolator messageInterpolator = new ResourceBundleMessageInterpolator();
private List<ValidationRule> rules = new ArrayList<>();
public Builder() {
// we start with the standard directive constraints to make us easier to use
addRules(DirectiveConstraints.STANDARD_CONSTRAINTS);
}
public Builder addRule(ValidationRule rule) {
rules.add(assertNotNull(rule));
return this;
}
public Builder addRules(Collection<? extends ValidationRule> rules) {
rules.forEach(this::addRule);
return this;
}
public Builder addRules(ValidationRule... rules) {
return addRules(Arrays.asList(rules));
}
public Builder clearRules() {
rules.clear();
return this;
}
public Builder messageInterpolator(MessageInterpolator messageInterpolator) {
this.messageInterpolator = assertNotNull(messageInterpolator);
return this;
}
/**
* This sets the locale of the validation rules. This is only needed while graphql-java does not allow you to get the
* locale from the {@link graphql.ExecutionInput}. A PR for this is in the works. Once that is available, then this method
* will not be as useful.
*
* @param locale the locale to use for message interpolation
*
* @return this builder
*/
public Builder locale(Locale locale) {
this.locale = locale;
return this;
}
public Builder onValidationErrorStrategy(OnValidationErrorStrategy onValidationErrorStrategy) {
this.onValidationErrorStrategy = assertNotNull(onValidationErrorStrategy);
return this;
}
public ValidationRules build() {
return new ValidationRules(this);
}
}
}