Skip to content

Commit 11e2433

Browse files
committed
Now with a Documentation meta data
1 parent 27f29d4 commit 11e2433

35 files changed

+526
-351
lines changed

readme.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ the `name` field must be at least 3 characters long and no more than 100 charact
5252

5353
The boolean value must be false.
5454

55-
- Example : `driver( isDrunk : Boolean @AssertFalse) : DriverDetails`
55+
- Example : `updateDriver( isDrunk : Boolean @AssertFalse) : DriverDetails`
5656

5757
- Applies to : `Boolean`
5858

@@ -65,7 +65,7 @@ The boolean value must be false.
6565

6666
The boolean value must be true.
6767

68-
- Example : `driver( hasLicence : Boolean @AssertTrue) : DriverDetails`
68+
- Example : `driveCar( hasLicence : Boolean @AssertTrue) : DriverDetails`
6969

7070
- Applies to : `Boolean`
7171

@@ -78,7 +78,7 @@ The boolean value must be true.
7878

7979
The element must be a number whose value must be less than or equal to the specified maximum.
8080

81-
- Example : `driver( bloodAlcoholLevel : Float @DecimalMax(value : "0.05") : DriverDetails`
81+
- Example : `driveCar( bloodAlcoholLevel : Float @DecimalMax(value : "0.05") : DriverDetails`
8282

8383
- Applies to : `String`, `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
8484

@@ -91,7 +91,7 @@ The element must be a number whose value must be less than or equal to the speci
9191

9292
The element must be a number whose value must be greater than or equal to the specified minimum.
9393

94-
- Example : `driver( carHorsePower : Float @DecimalMin(value : "300.50") : DriverDetails`
94+
- Example : `driveCar( carHorsePower : Float @DecimalMin(value : "300.50") : DriverDetails`
9595

9696
- Applies to : `String`, `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
9797

@@ -104,7 +104,7 @@ The element must be a number whose value must be greater than or equal to the sp
104104

105105
The element must be a number inside the specified `integer` and `fraction` range.
106106

107-
- Example : `driver( carCost : Float @Digits(integer : 5, fraction : 2) : DriverDetails`
107+
- Example : `buyCar( carCost : Float @Digits(integer : 5, fraction : 2) : DriverDetails`
108108

109109
- Applies to : `String`, `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
110110

@@ -117,7 +117,7 @@ The element must be a number inside the specified `integer` and `fraction` range
117117

118118
The element must be a number whose value must be less than or equal to the specified maximum.
119119

120-
- Example : `driver( horsePower : Float @Max(value : 1000) : DriverDetails`
120+
- Example : `driveCar( horsePower : Float @Max(value : 1000) : DriverDetails`
121121

122122
- Applies to : `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
123123

@@ -130,7 +130,7 @@ The element must be a number whose value must be less than or equal to the speci
130130

131131
The element must be a number whose value must be greater than or equal to the specified minimum.
132132

133-
- Example : `driver( age : Int @Min(value : 18) : DriverDetails`
133+
- Example : `driveCar( age : Int @Min(value : 18) : DriverDetails`
134134

135135
- Applies to : `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
136136

@@ -143,7 +143,7 @@ The element must be a number whose value must be greater than or equal to the sp
143143

144144
The element must be a negative number.
145145

146-
- Example : `driver( licencePoints : Int @Negative) : DriverDetails`
146+
- Example : `driveCar( lostLicencePoints : Int @Negative) : DriverDetails`
147147

148148
- Applies to : `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
149149

@@ -156,7 +156,7 @@ The element must be a negative number.
156156

157157
The element must be a negative number or zero.
158158

159-
- Example : `driver( licencePoints : Int @NegativeOrZero) : DriverDetails`
159+
- Example : `driveCar( lostLicencePoints : Int @NegativeOrZero) : DriverDetails`
160160

161161
- Applies to : `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
162162

@@ -247,10 +247,10 @@ The element range must be between the specified `min` and `max` boundaries (incl
247247

248248
The element size must be between the specified `min` and `max` boundaries (inclusive).
249249

250-
- Example : `updateDriver( drivingNote : String @Size( min : 1000, max : 100000)) : DriverDetails`
250+
- Example : `updateDrivingNotes( drivingNote : String @Size( min : 1000, max : 100000)) : DriverDetails`
251251

252252
- Applies to : `String`, `Lists`, `Input Objects`
253253

254254
- SDL : directive @Size(min : Int = 0, max : Int = 2147483647, message : String = "graphql.validation.Size.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
255255

256-
- Message : `graphql.validation.Size.message`
256+
- Message : `graphql.validation.Size.message`

src/main/java/graphql/validation/constraints/AbstractDirectiveConstraint.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import graphql.GraphQLError;
55
import graphql.PublicSpi;
66
import graphql.Scalars;
7+
import graphql.execution.ExecutionPath;
78
import graphql.schema.GraphQLArgument;
89
import graphql.schema.GraphQLDirective;
910
import graphql.schema.GraphQLFieldDefinition;
@@ -13,6 +14,7 @@
1314
import graphql.schema.GraphQLScalarType;
1415
import graphql.schema.GraphQLTypeUtil;
1516
import graphql.validation.rules.ValidationEnvironment;
17+
import graphql.validation.util.DirectivesAndTypeWalker;
1618
import graphql.validation.util.Util;
1719

1820
import java.lang.reflect.Array;
@@ -46,8 +48,7 @@ public String getName() {
4648
}
4749

4850

49-
@Override
50-
public String getMessageTemplate() {
51+
protected String getMessageTemplate() {
5152
return "graphql.validation." + getName() + ".message";
5253
}
5354

@@ -191,16 +192,17 @@ protected String getMessageTemplate(GraphQLDirective directive) {
191192
/**
192193
* Creates a map of named parameters for message interpolation
193194
*
194-
* @param validatedValue the value being validated
195-
* @param ruleEnvironment the rule environment
196-
* @param args must be even with a String as even params and values as odd params
195+
* @param validatedValue the value being validated
196+
* @param validationEnvironment the validation environment
197+
* @param args must be an key / value array with String keys as the even params and values as then odd params
197198
*
198199
* @return a map of message parameters
199200
*/
200-
protected Map<String, Object> mkMessageParams(Object validatedValue, ValidationEnvironment ruleEnvironment, Object... args) {
201+
protected Map<String, Object> mkMessageParams(Object validatedValue, ValidationEnvironment validationEnvironment, Object... args) {
201202
Map<String, Object> params = new LinkedHashMap<>();
202203
params.put("validatedValue", validatedValue);
203204
params.put("constraint", getName());
205+
params.put("path", mkFieldOrArgPath(validationEnvironment));
204206

205207
Assert.assertTrue(args.length % 2 == 0, "You MUST pass in an even number of arguments");
206208
for (int ix = 0; ix < args.length; ix = ix + 2) {
@@ -212,12 +214,20 @@ protected Map<String, Object> mkMessageParams(Object validatedValue, ValidationE
212214
return params;
213215
}
214216

217+
private Object mkFieldOrArgPath(ValidationEnvironment validationEnvironment) {
218+
ExecutionPath executionPath = validationEnvironment.getExecutionPath();
219+
ExecutionPath fieldOrArgumentPath = validationEnvironment.getFieldOrArgumentPath();
220+
221+
executionPath = Util.concatPaths(executionPath, fieldOrArgumentPath);
222+
return executionPath == null ? "/" : executionPath.toString();
223+
}
224+
215225
/**
216226
* Creates a new {@link graphql.GraphQLError}
217227
*
218228
* @param validationEnvironment the current validation environment
219-
* @param directive the directive being run
220-
* @param msgParams the map of parameters
229+
* @param directive the directive being run
230+
* @param msgParams the map of parameters
221231
*
222232
* @return a list of a single error
223233
*/

src/main/java/graphql/validation/constraints/DirectiveConstraint.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,10 @@ public interface DirectiveConstraint extends ValidationRule {
1818
*/
1919
String getName();
2020

21-
/**
22-
* @return a description for documentation
23-
*/
24-
String getDescription();
25-
26-
/**
27-
* @return a description for documentation
28-
*/
29-
String getExample();
30-
31-
/**
32-
* @return the graphql SDL directive declaration syntax
33-
*/
34-
String getDirectiveDeclarationSDL();
35-
36-
/**
37-
* @return the message template name
38-
*/
39-
String getMessageTemplate();
4021

4122
/**
42-
* @return a list of the names of applicable types
23+
* @return documentation meta data about this constraint
4324
*/
44-
List<String> getApplicableTypeNames();
25+
Documentation getDocumentation();
4526

4627
}

src/main/java/graphql/validation/constraints/DirectiveConstraints.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
import graphql.validation.constraints.standard.PositiveOrZeroConstraint;
3232
import graphql.validation.constraints.standard.RangeConstraint;
3333
import graphql.validation.constraints.standard.SizeConstraint;
34-
import graphql.validation.rules.ValidationRule;
3534
import graphql.validation.rules.ValidationEnvironment;
35+
import graphql.validation.rules.ValidationRule;
3636
import graphql.validation.util.Util;
3737

3838
import java.util.ArrayList;
@@ -42,10 +42,16 @@
4242
import java.util.List;
4343
import java.util.Map;
4444

45+
/**
46+
* This contains a liszt of {@link graphql.validation.constraints.DirectiveConstraint}s and
47+
* runs them as a group on a field and its argument values.
48+
* <p>
49+
* This ships with a set of standard constraints via {@link #STANDARD_CONSTRAINTS} but you can
50+
* add your own implementations if you wish
51+
*/
4552
@PublicApi
4653
public class DirectiveConstraints implements ValidationRule {
4754

48-
4955
/**
5056
* These are the standard directive rules that come with the system
5157
*/
@@ -82,10 +88,10 @@ public Map<String, DirectiveConstraint> getConstraints() {
8288
return constraints;
8389
}
8490

85-
public String getDirectivesDeclarationSDL() {
91+
public String getDirectivesSDL() {
8692
StringBuilder sb = new StringBuilder();
87-
for (DirectiveConstraint value : constraints.values()) {
88-
sb.append("\n ").append(value.getDirectiveDeclarationSDL()).append("\n");
93+
for (DirectiveConstraint directiveConstraint : constraints.values()) {
94+
sb.append("\n ").append(directiveConstraint.getDocumentation().getDirectiveSDL()).append("\n");
8995
}
9096
return sb.toString();
9197
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package graphql.validation.constraints;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class Documentation {
7+
8+
private final String description;
9+
private final String example;
10+
private final String directiveSDL;
11+
private final String messageTemplate;
12+
private final List<String> applicableTypeNames;
13+
14+
private Documentation(Builder builder) {
15+
this.description = builder.description;
16+
this.example = builder.example;
17+
this.directiveSDL = builder.directiveSDL;
18+
this.messageTemplate = builder.messageTemplate;
19+
this.applicableTypeNames = builder.applicableTypeNames;
20+
}
21+
22+
public static Builder newDocumentation() {
23+
return new Builder();
24+
}
25+
26+
public String getDescription() {
27+
return description;
28+
}
29+
30+
public String getExample() {
31+
return example;
32+
}
33+
34+
public String getDirectiveSDL() {
35+
return directiveSDL;
36+
}
37+
38+
public String getMessageTemplate() {
39+
return messageTemplate;
40+
}
41+
42+
public List<String> getApplicableTypeNames() {
43+
return applicableTypeNames;
44+
}
45+
46+
public static class Builder {
47+
private String description;
48+
private String example;
49+
private String directiveSDL;
50+
private String messageTemplate;
51+
private List<String> applicableTypeNames;
52+
53+
54+
public Builder description(String description) {
55+
this.description = description;
56+
return this;
57+
}
58+
59+
public Builder example(String example) {
60+
this.example = example;
61+
return this;
62+
}
63+
64+
public Builder directiveSDL(String format, Object... args) {
65+
this.directiveSDL = String.format(format, args);
66+
return this;
67+
}
68+
69+
public Builder messageTemplate(String messageTemplate) {
70+
this.messageTemplate = messageTemplate;
71+
return this;
72+
}
73+
74+
public Builder applicableTypeNames(List<String> applicableTypeNames) {
75+
this.applicableTypeNames = applicableTypeNames;
76+
return this;
77+
}
78+
79+
public Builder applicableTypeNames(String... applicableTypeNames) {
80+
this.applicableTypeNames = Arrays.asList(applicableTypeNames);
81+
return this;
82+
}
83+
84+
public Documentation build() {
85+
return new Documentation(this);
86+
}
87+
88+
}
89+
}

src/main/java/graphql/validation/constraints/standard/AbstractAssertConstraint.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
import graphql.GraphQLError;
44
import graphql.schema.GraphQLDirective;
55
import graphql.schema.GraphQLInputType;
6-
import graphql.schema.GraphQLScalarType;
76
import graphql.validation.constraints.AbstractDirectiveConstraint;
87
import graphql.validation.rules.ValidationEnvironment;
98

109
import java.util.Collections;
1110
import java.util.List;
12-
import java.util.stream.Collectors;
13-
import java.util.stream.Stream;
1411

1512
import static graphql.Scalars.GraphQLBoolean;
1613

@@ -28,11 +25,6 @@ public boolean appliesToType(GraphQLInputType inputType) {
2825
);
2926
}
3027

31-
@Override
32-
public List<String> getApplicableTypeNames() {
33-
return Stream.of(GraphQLBoolean).map(GraphQLScalarType::getName).collect(Collectors.toList());
34-
}
35-
3628
@Override
3729
public List<GraphQLError> runValidation(ValidationEnvironment validationEnvironment) {
3830
Object validatedValue = validationEnvironment.getValidatedValue();

src/main/java/graphql/validation/constraints/standard/AbstractDecimalMinMaxConstraint.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public boolean appliesToType(GraphQLInputType inputType) {
3434
);
3535
}
3636

37-
@Override
3837
public List<String> getApplicableTypeNames() {
3938
return Stream.of(Scalars.GraphQLString, // note we allow strings
4039
Scalars.GraphQLByte,

src/main/java/graphql/validation/constraints/standard/AbstractMinMaxConstraint.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public boolean appliesToType(GraphQLInputType inputType) {
3434
);
3535
}
3636

37-
@Override
3837
public List<String> getApplicableTypeNames() {
3938
return Stream.of(Scalars.GraphQLByte,
4039
Scalars.GraphQLShort,

src/main/java/graphql/validation/constraints/standard/AbstractPositiveNegativeConstraint.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public boolean appliesToType(GraphQLInputType inputType) {
3434
);
3535
}
3636

37-
@Override
3837
public List<String> getApplicableTypeNames() {
3938
return Stream.of(Scalars.GraphQLByte,
4039
Scalars.GraphQLShort,

0 commit comments

Comments
 (0)