1
1
# Extended Validation for graphql-java
2
2
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 )
4
4
5
5
6
6
# Status
@@ -13,16 +13,14 @@ But the project welcomes all feedback and input on code design and validation re
13
13
14
14
TODO
15
15
16
- # SDL @Directive validation
16
+ # SDL @Directive constraints
17
17
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.
19
19
20
20
These names and semantics are inspired from the javax.validation annotations
21
21
22
22
https://javaee.github.io/javaee-spec/javadocs/javax/validation/constraints/package-summary.html
23
23
24
- ## SDL directives
25
-
26
24
You can add these onto arguments or input types in your graphql SDL.
27
25
28
26
For example
@@ -44,113 +42,215 @@ For example
44
42
}
45
43
```
46
44
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 ,
48
46
the `name ` field must be at least 3 characters long and no more than 100 characters long to be considered valid .
49
47
50
48
51
- ## The library supplied directives
49
+ ## The supplied constraints
52
50
53
- ### AssertFalse
51
+ ### @ AssertFalse
54
52
55
53
The boolean value must be false .
56
54
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
+
59
63
60
- ### AssertTrue
64
+ ### @ AssertTrue
61
65
62
66
The boolean value must be true .
63
67
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
+
66
76
67
- ### DecimalMax
77
+ ### @ DecimalMax
68
78
69
79
The element must be a number whose value must be less than or equal to the specified maximum .
70
80
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 `
73
82
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
75
91
76
92
The element must be a number whose value must be greater than or equal to the specified minimum .
77
93
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
+
80
102
81
- ### Digits
103
+ ### @ Digits
82
104
83
105
The element must be a number inside the specified `integer ` and `fraction ` range .
84
106
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 `
87
108
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
89
117
90
118
The element must be a number whose value must be less than or equal to the specified maximum .
91
119
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 `
94
123
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
96
130
97
131
The element must be a number whose value must be greater than or equal to the specified minimum .
98
132
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 `
101
136
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
103
143
104
144
The element must be a negative number .
105
145
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 `
108
149
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
110
156
111
157
The element must be a negative number or zero .
112
158
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 `
115
162
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
117
169
118
170
The String must contain at least one non -whitespace character , according to Java 's Character .isWhitespace ().
119
171
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
122
177
123
- ### NotEmpty
178
+ - Message : `graphql .validation .NotBlank .message `
179
+
180
+
181
+ ### @NotEmpty
124
182
125
183
The element must have a non zero size .
126
184
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
129
190
130
- ### Pattern
191
+ - Message : `graphql .validation .NotEmpty .message `
192
+
193
+
194
+ ### @Pattern
131
195
132
196
The String must match the specified regular expression , which follows the Java regular expression conventions .
133
197
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
136
203
137
- ### Positive
204
+ - Message : `graphql .validation .Pattern .message `
205
+
206
+
207
+ ### @Positive
138
208
139
209
The element must be a positive number .
140
210
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
143
216
144
- ### PositiveOrZero
217
+ - Message : `graphql .validation .Positive .message `
218
+
219
+
220
+ ### @PositiveOrZero
145
221
146
222
The element must be a positive number or zero .
147
223
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 .
150
236
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
152
247
153
248
The element size must be between the specified `min ` and `max ` boundaries (inclusive).
154
249
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 `
0 commit comments