Skip to content

Commit 27f29d4

Browse files
committed
Now with examples
1 parent e30f265 commit 27f29d4

19 files changed

+265
-58
lines changed

readme.md

Lines changed: 152 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Extended Validation for graphql-java
22

3-
This library provides extended validation for [graphql-java](https://github.com/graphql-java/graphql-java)
3+
This library provides extended validation of fields and field arguments for [graphql-java](https://github.com/graphql-java/graphql-java)
44

55

66
# Status
@@ -13,16 +13,14 @@ But the project welcomes all feedback and input on code design and validation re
1313

1414
TODO
1515

16-
# SDL @Directive validation
16+
# SDL @Directive constraints
1717

18-
This library a series of directives that can be applied to field arguments and input type fields which will constrain the values
18+
This library a series of directives that can be applied to field arguments and input type fields which will constrain their allowable values.
1919

2020
These names and semantics are inspired from the javax.validation annotations
2121

2222
https://javaee.github.io/javaee-spec/javadocs/javax/validation/constraints/package-summary.html
2323

24-
## SDL directives
25-
2624
You can add these onto arguments or input types in your graphql SDL.
2725

2826
For example
@@ -44,113 +42,215 @@ For example
4442
}
4543
```
4644

47-
In the example above, we have a `applications` argument that takes at most 10 applications and within each `Application` input object
45+
In the example above, we have a `applications` argument that takes at most 10 applications and within each `Application` input object,
4846
the `name` field must be at least 3 characters long and no more than 100 characters long to be considered valid.
4947

5048

51-
## The library supplied directives
49+
## The supplied constraints
5250

53-
### AssertFalse
51+
### @AssertFalse
5452

5553
The boolean value must be false.
5654

57-
- SDL : `directive @AssertFalse(message : String = "graphql.validation.AssertFalse.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`
58-
- Applies to : `Boolean`
55+
- Example : `driver( isDrunk : Boolean @AssertFalse) : DriverDetails`
56+
57+
- Applies to : `Boolean`
58+
59+
- SDL : directive @AssertFalse(message : String = "graphql.validation.AssertFalse.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
60+
61+
- Message : `graphql.validation.AssertFalse.message`
62+
5963

60-
### AssertTrue
64+
### @AssertTrue
6165

6266
The boolean value must be true.
6367

64-
- SDL : `directive @AssertTrue(message : String = "graphql.validation.AssertTrue.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`
65-
- Applies to : `Boolean`
68+
- Example : `driver( hasLicence : Boolean @AssertTrue) : DriverDetails`
69+
70+
- Applies to : `Boolean`
71+
72+
- SDL : directive @AssertTrue(message : String = "graphql.validation.AssertTrue.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
73+
74+
- Message : `graphql.validation.AssertTrue.message`
75+
6676

67-
### DecimalMax
77+
### @DecimalMax
6878

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

71-
- SDL : `directive @DecimalMax(value : String!, inclusive : Boolean! = true, message : String = "graphql.validation.DecimalMax.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`
72-
- Applies to : `String`, `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
81+
- Example : `driver( bloodAlcoholLevel : Float @DecimalMax(value : "0.05") : DriverDetails`
7382

74-
### DecimalMin
83+
- Applies to : `String`, `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
84+
85+
- SDL : directive @DecimalMax(value : String!, inclusive : Boolean! = true, message : String = "graphql.validation.DecimalMax.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
86+
87+
- Message : `graphql.validation.DecimalMax.message`
88+
89+
90+
### @DecimalMin
7591

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

78-
- SDL : `directive @DecimalMin(value : String!, inclusive : Boolean! = true, message : String = "graphql.validation.DecimalMin.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`
79-
- Applies to : `String`, `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
94+
- Example : `driver( carHorsePower : Float @DecimalMin(value : "300.50") : DriverDetails`
95+
96+
- Applies to : `String`, `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
97+
98+
- SDL : directive @DecimalMin(value : String!, inclusive : Boolean! = true, message : String = "graphql.validation.DecimalMin.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
99+
100+
- Message : `graphql.validation.DecimalMin.message`
101+
80102

81-
### Digits
103+
### @Digits
82104

83105
The element must be a number inside the specified `integer` and `fraction` range.
84106

85-
- SDL : `directive @Digits(integer : Int!, fraction : Int!, message : String = "graphql.validation.Digits.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`
86-
- Applies to : `String`, `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
107+
- Example : `driver( carCost : Float @Digits(integer : 5, fraction : 2) : DriverDetails`
87108

88-
### Max
109+
- Applies to : `String`, `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
110+
111+
- SDL : directive @Digits(integer : Int!, fraction : Int!, message : String = "graphql.validation.Digits.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
112+
113+
- Message : `graphql.validation.Digits.message`
114+
115+
116+
### @Max
89117

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

92-
- SDL : `directive @Max(value : Int! = 2147483647, message : String = "graphql.validation.Max.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`
93-
- Applies to : `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
120+
- Example : `driver( horsePower : Float @Max(value : 1000) : DriverDetails`
121+
122+
- Applies to : `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
94123

95-
### Min
124+
- SDL : directive @Max(value : Int! = 2147483647, message : String = "graphql.validation.Max.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
125+
126+
- Message : `graphql.validation.Max.message`
127+
128+
129+
### @Min
96130

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

99-
- SDL : `directive @Min(value : Int! = 0, message : String = "graphql.validation.Min.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`
100-
- Applies to : `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
133+
- Example : `driver( age : Int @Min(value : 18) : DriverDetails`
134+
135+
- Applies to : `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
101136

102-
### Negative
137+
- SDL : directive @Min(value : Int! = 0, message : String = "graphql.validation.Min.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
138+
139+
- Message : `graphql.validation.Min.message`
140+
141+
142+
### @Negative
103143

104144
The element must be a negative number.
105145

106-
- SDL : `directive @Negative(message : String = "graphql.validation.Negative.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`
107-
- Applies to : `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
146+
- Example : `driver( licencePoints : Int @Negative) : DriverDetails`
147+
148+
- Applies to : `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
108149

109-
### NegativeOrZero
150+
- SDL : directive @Negative(message : String = "graphql.validation.Negative.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
151+
152+
- Message : `graphql.validation.Negative.message`
153+
154+
155+
### @NegativeOrZero
110156

111157
The element must be a negative number or zero.
112158

113-
- SDL : `directive @NegativeOrZero(message : String = "graphql.validation.NegativeOrZero.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`
114-
- Applies to : `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
159+
- Example : `driver( licencePoints : Int @NegativeOrZero) : DriverDetails`
160+
161+
- Applies to : `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
115162

116-
### NotBlank
163+
- SDL : directive @NegativeOrZero(message : String = "graphql.validation.NegativeOrZero.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
164+
165+
- Message : `graphql.validation.NegativeOrZero.message`
166+
167+
168+
### @NotBlank
117169

118170
The String must contain at least one non-whitespace character, according to Java's Character.isWhitespace().
119171

120-
- SDL : `directive @NotBlank(message : String = "graphql.validation.NotBlank.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`
121-
- Applies to : `String`
172+
- Example : `updateAccident( accidentNotes : String @NotBlank) : DriverDetails`
173+
174+
- Applies to : `String`
175+
176+
- SDL : directive @NotBlank(message : String = "graphql.validation.NotBlank.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
122177

123-
### NotEmpty
178+
- Message : `graphql.validation.NotBlank.message`
179+
180+
181+
### @NotEmpty
124182

125183
The element must have a non zero size.
126184

127-
- SDL : `directive @NotEmpty(message : String = "graphql.validation.NotEmpty.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`
128-
- Applies to : `String`, `Lists`, `Input Objects`
185+
- Example : `updateAccident( accidentNotes : [Notes]! @NotEmpty) : DriverDetails`
186+
187+
- Applies to : `String`, `Lists`, `Input Objects`
188+
189+
- SDL : directive @NotEmpty(message : String = "graphql.validation.NotEmpty.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
129190

130-
### Pattern
191+
- Message : `graphql.validation.NotEmpty.message`
192+
193+
194+
### @Pattern
131195

132196
The String must match the specified regular expression, which follows the Java regular expression conventions.
133197

134-
- SDL : `directive @Pattern(pattern : String! =".*", message : String = "graphql.validation.Pattern.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`
135-
- Applies to : `String`
198+
- Example : `updateDriver( licencePlate : String @Patttern(regex : "[A-Z][A-Z][A-Z]-[0-9][0-9][0-9]") : DriverDetails`
199+
200+
- Applies to : `String`
201+
202+
- SDL : directive @Pattern(regexp : String! =".*", message : String = "graphql.validation.Pattern.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
136203

137-
### Positive
204+
- Message : `graphql.validation.Pattern.message`
205+
206+
207+
### @Positive
138208

139209
The element must be a positive number.
140210

141-
- SDL : `directive @Positive(message : String = "graphql.validation.Positive.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`
142-
- Applies to : `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
211+
- Example : `driver( licencePoints : Int @Positive) : DriverDetails`
212+
213+
- Applies to : `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
214+
215+
- SDL : directive @Positive(message : String = "graphql.validation.Positive.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
143216

144-
### PositiveOrZero
217+
- Message : `graphql.validation.Positive.message`
218+
219+
220+
### @PositiveOrZero
145221

146222
The element must be a positive number or zero.
147223

148-
- SDL : `directive @PositiveOrZero(message : String = "graphql.validation.PositiveOrZero.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`
149-
- Applies to : `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
224+
- Example : `driver( licencePoints : Int @PositiveOrZero) : DriverDetails`
225+
226+
- Applies to : `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
227+
228+
- SDL : directive @PositiveOrZero(message : String = "graphql.validation.PositiveOrZero.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
229+
230+
- Message : `graphql.validation.PositiveOrZero.message`
231+
232+
233+
### @Range
234+
235+
The element range must be between the specified `min` and `max` boundaries (inclusive). It accepts numbers and strings that represent numerical values.
150236

151-
### Size
237+
- Example : `driver( milesTravelled : Int @Range( min : 1000, max : 100000)) : DriverDetails`
238+
239+
- Applies to : `String`, `Byte`, `Short`, `Int`, `Long`, `BigDecimal`, `BigInteger`, `Float`
240+
241+
- SDL : directive @Range(min : Int = 0, max : Int = 2147483647, message : String = "graphql.validation.Range.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
242+
243+
- Message : `graphql.validation.Range.message`
244+
245+
246+
### @Size
152247

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

155-
- SDL : `directive @Size(min : Int = 0, max : Int = 2147483647, message : String = "graphql.validation.Size.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`
156-
- Applies to : `String`, `Lists`, `Input Objects`
250+
- Example : `updateDriver( drivingNote : String @Size( min : 1000, max : 100000)) : DriverDetails`
251+
252+
- Applies to : `String`, `Lists`, `Input Objects`
253+
254+
- SDL : directive @Size(min : Int = 0, max : Int = 2147483647, message : String = "graphql.validation.Size.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
255+
256+
- Message : `graphql.validation.Size.message`

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,34 @@
1313
@PublicSpi
1414
public interface DirectiveConstraint extends ValidationRule {
1515

16+
/**
17+
* @return the name of the constraint
18+
*/
1619
String getName();
1720

21+
/**
22+
* @return a description for documentation
23+
*/
1824
String getDescription();
1925

26+
/**
27+
* @return a description for documentation
28+
*/
29+
String getExample();
30+
31+
/**
32+
* @return the graphql SDL directive declaration syntax
33+
*/
2034
String getDirectiveDeclarationSDL();
2135

36+
/**
37+
* @return the message template name
38+
*/
2239
String getMessageTemplate();
2340

41+
/**
42+
* @return a list of the names of applicable types
43+
*/
2444
List<String> getApplicableTypeNames();
2545

2646
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public String getDirectiveDeclarationSDL() {
1313
getMessageTemplate());
1414
}
1515

16+
@Override
17+
public String getExample() {
18+
return "driver( isDrunk : Boolean @AssertFalse) : DriverDetails";
19+
}
20+
1621
@Override
1722
public String getDescription() {
1823
return "The boolean value must be false.";

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public String getDirectiveDeclarationSDL() {
1313
getMessageTemplate());
1414
}
1515

16+
@Override
17+
public String getExample() {
18+
return "driver( hasLicence : Boolean @AssertTrue) : DriverDetails";
19+
}
20+
1621
@Override
1722
public String getDescription() {
1823
return "The boolean value must be true.";

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public String getDirectiveDeclarationSDL() {
1717
public String getDescription() {
1818
return "The element must be a number whose value must be less than or equal to the specified maximum.";
1919
}
20+
21+
@Override
22+
public String getExample() {
23+
return "driver( bloodAlcoholLevel : Float @DecimalMax(value : \"0.05\") : DriverDetails";
24+
}
25+
2026
@Override
2127
protected boolean isOK(boolean inclusive, int comparisonResult) {
2228
return inclusive ? comparisonResult <= 0 : comparisonResult < 0;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public String getDescription() {
1818
return "The element must be a number whose value must be greater than or equal to the specified minimum.";
1919
}
2020

21+
@Override
22+
public String getExample() {
23+
return "driver( carHorsePower : Float @DecimalMin(value : \"300.50\") : DriverDetails";
24+
}
25+
26+
2127
@Override
2228
protected boolean isOK(boolean inclusive, int comparisonResult) {
2329
return inclusive ? comparisonResult >= 0 : comparisonResult > 0;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ public String getDescription() {
6161
return "The element must be a number inside the specified `integer` and `fraction` range.";
6262
}
6363

64+
@Override
65+
public String getExample() {
66+
return "driver( carCost : Float @Digits(integer : 5, fraction : 2) : DriverDetails";
67+
}
68+
69+
6470
@Override
6571
public List<GraphQLError> runValidation(ValidationEnvironment validationEnvironment) {
6672
Object validatedValue = validationEnvironment.getValidatedValue();

0 commit comments

Comments
 (0)