1
1
# Jakarta Extension
2
2
3
- The ` litecommands-jakarta ` is a extension for supporting Jakarta EE annotation validation.
3
+ The ` litecommands-jakarta ` is a extension for supporting Jakarta EE annotation validation.
4
+ It allows you to use Jakarta EE Bean Validation annotations in the command arguments:
4
5
5
6
``` Java
6
7
@Execute
7
- void ban(@Arg @Pattern (regexp = " [a-zA-Z_]+" ) String name)
8
+ void ban(@Arg @Size (min = 3 , max = 16 ) String nick) {
9
+ // Command implementation
10
+ }
8
11
```
9
12
10
- ## Dependency
13
+ ## 0. Dependency
11
14
Add the following dependency to your project:
12
15
13
16
<tabs >
@@ -30,15 +33,217 @@ implementation("dev.rollczi:litecommands-adventure:%latest_version%")
30
33
</tabs >
31
34
32
35
33
- ## Registering the extension
36
+ ## 1. Register Jakarta extension
34
37
35
38
Register the extension in the ` LiteCommands ` builder:
36
39
40
+ <tabs >
41
+
42
+ <tab title =" Jakarta Extension " >
43
+
44
+ ``` java
45
+ .extension(new LiteJakartaExtension<> (), config - > config
46
+ // jakarta config ...
47
+ )
48
+ ```
49
+ </tab >
50
+
51
+ <tab title =" Jakarta Extension (without config) " >
52
+
37
53
``` java
38
54
.extension(new LiteJakartaExtension<> ())
39
55
```
56
+ </tab >
57
+
58
+ </tabs >
59
+
60
+ Before configuration, it is worth knowing how the Jakarta Validation works in LiteCommands.
61
+
62
+ Suppose we have a command with a ` nick ` argument annotated with ` @Size ` and ` @Pattern ` annotations.
63
+
64
+ <img src =" litecommands-jakarta.png " alt =" litecommands jakarta " width =" 700 " />
65
+
66
+ ** 1*** - Argument ` nick ` (String)
67
+ ** 2*** - ` @Size ` jakarta annotation (between 3 and 16 characters)
68
+ ** 3*** - ` @Pattern ` jakarta annotation (only letters)
69
+ ** 4*** - Jakarta Validation (` org.hibernate.validator:hibernate-validator ` implementation)
70
+ ** 5*** - Result of the validation (contains all violations)
71
+ ** 6*** - Constraint violations message (joined header with all violation messages)
72
+ ** 7*** - Violation message (specific message for each violation type)
73
+
74
+ See documentation for [ Jakarta EE Bean Validation] ( https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/package-summary ) to learn more about jakarta validation annotations.
75
+
76
+ ## 2. Configure Jakarta messages
77
+
78
+ ### 2.1. Constraint violations message
79
+
80
+ The ` .constraintViolationsMessage() ` method allows you to define a message for all constraint violations.
81
+
82
+ For example, we can define a message for all violations, which will contain the header and all violation messages.
83
+ The ` result.asJoinedString() ` method joins all violation messages into one string.
84
+
85
+ ``` Java
86
+ .constraintViolationsMessage((invocation, result) - >
87
+ " [!] Custom constraint violations: \n " + result. asJoinedString(" \n " )
88
+ )
89
+ ```
40
90
41
- ## Example
91
+ Result of the method will be:
92
+
93
+ ``` Java
94
+ [! ] Custom constraint violations:
95
+ < nick> - size must be between 3 and 16
96
+ < nick> - must match " [a-zA-Z]*"
97
+ ```
98
+
99
+ ### 2.2. Global violation message
100
+
101
+ The ` .violationMessage() ` method allows you to define a message for all violations.
102
+
103
+ For example, we can define a message for all violations, which will contain the parameter name and the violation message.
104
+
105
+ ``` Java
106
+ .violationMessage((invocation, violation) - >
107
+ " - " + violation. getParameterName() + " : " + violation. getMessage()
108
+ )
109
+ ```
110
+
111
+ Result of the method will be:
112
+
113
+ ``` Java
114
+ [! ] Custom constraint violations:
115
+ - nick: size must be between 3 and 16
116
+ - nick: must match " [a-zA-Z]*"
117
+ ```
118
+
119
+ ### 2.3. Violation messages for specific annotations
120
+
121
+ The ` .violationMessage() ` method allows you to define a message for specific annotations.
122
+
123
+ For example, we can define a message for the ` @Size ` annotation.
124
+ The ` violation.getAnnotation() ` returns ` @Size ` annotation instance and allows you to get the annotation values.
125
+
126
+ ``` Java
127
+ .violationMessage(Size . class, (invocation, violation) - > {
128
+ Size size = violation. getAnnotation();
129
+ int min = size. min();
130
+ int max = size. max();
131
+ String name = violation. getParameterName();
132
+
133
+ return " - " + name + " : between " + min + " and " + max;
134
+ })
135
+ ```
136
+
137
+ Result of the method will be:
138
+
139
+ ``` Java
140
+ [! ] Custom constraint violations:
141
+ - nick: between 3 and 16
142
+ - nick: must match " [a-zA-Z]*"
143
+ ```
144
+
145
+ ### 2.4. Violation messages for specific annotations in specific locale
146
+
147
+ First, we need to define how to get the locale for a command invocation.
148
+
149
+ ``` Java
150
+ .locale(invocation - > {
151
+ Locale locale = invocation. sender(). getLocale();
152
+
153
+ return locale != null ? locale : Locale . ENGLISH ;
154
+ })
155
+ ```
156
+
157
+ The ` .violationMessage() ` method allows you to define a message for specific annotations in specific locale.
158
+
159
+ For example, we can define a message for the ` @Pattern ` annotation in the English locale.
160
+
161
+ ``` Java
162
+ .violationMessage(Pattern . class, (invocation, violation) - > {
163
+ Pattern pattern = violation. getAnnotation();
164
+ String name = violation. getParameterName();
165
+
166
+ return Locale . ENGLISH. equals(violation. getLocale())
167
+ ? " - " + name + " : [English] must match " + pattern. regexp()
168
+ : " - " + name + " : [Polish] musi pasować do " + pattern. regexp();
169
+ })
170
+ ```
171
+
172
+ Result for the English locale will be:
173
+
174
+ ``` Java
175
+ [! ] Custom constraint violations:
176
+ - nick: [English ] must match " [a-zA-Z]*"
177
+ - nick: size must be between 3 and 16
178
+ ```
179
+
180
+ Result for the other locale will be:
181
+
182
+ ``` Java
183
+ [! ] Custom constraint violations:
184
+ - nick: [Polish ] musi pasować do " [a-zA-Z]*"
185
+ - nick: size must be between 3 and 16
186
+ ```
187
+
188
+ ## 3. Full configuration
189
+
190
+ ``` Java
191
+ .extension(new LiteJakartaExtension<> (), config - > config
192
+ // locale
193
+ .locale(invocation - > {
194
+ Locale locale = invocation. sender(). getLocale();
195
+
196
+ return locale != null ? locale : Locale . ENGLISH ;
197
+ })
198
+
199
+ // constraint violations message
200
+ .constraintViolationsMessage((invocation, result) - >
201
+ " [!] Custom constraint violations: \n " + result. asJoinedString(" \n " )
202
+ )
203
+
204
+ // global violation message
205
+ .violationMessage((invocation, violation) - >
206
+ " - " + violation. getParameterName() + " : " + violation. getMessage()
207
+ )
208
+
209
+ // violation message for specific annotations
210
+ .violationMessage(Size . class, (invocation, violation) - > {
211
+ Size size = violation. getAnnotation();
212
+ int min = size. min();
213
+ int max = size. max();
214
+ String name = violation. getParameterName();
215
+
216
+ return " - " + name + " : between " + min + " and " + max;
217
+ })
218
+
219
+ .violationMessage(Pattern . class, (invocation, violation) - > {
220
+ Pattern pattern = violation. getAnnotation();
221
+ String name = violation. getParameterName();
222
+
223
+ return Locale . ENGLISH. equals(violation. getLocale())
224
+ ? " - " + name + " : [English] must match " + pattern. regexp()
225
+ : " - " + name + " : [Polish] musi pasować do " + pattern. regexp();
226
+ })
227
+ )
228
+ ```
229
+
230
+ ## 4. Advanced configuration
231
+
232
+ ### 4.1. Custom validation factory
233
+
234
+ The ` .validationFactory() ` method allows you to define a custom validation factory.
235
+
236
+ For example, we can define a custom validation factory that will use the ` org.hibernate.validator:hibernate-validator ` implementation.
237
+
238
+ ``` Java
239
+ .validationFactory(Validation . byProvider(HibernateValidator . class)
240
+ .configure()
241
+ // hibernate validator config ...
242
+ .buildValidatorFactory()
243
+ )
244
+ ```
245
+
246
+ ## Other examples
42
247
43
248
``` Java
44
249
@Command (name = " ban" )
@@ -47,38 +252,51 @@ public class BanCommand {
47
252
@Execute
48
253
void ban (
49
254
@Arg @Pattern (regexp = " [a-zA-Z_]+" ) String name ,
50
- @Arg @Future Instant date ,
255
+ @Arg @Future Instant date
51
256
) {
52
257
// Command implementation
53
258
}
54
259
55
260
}
56
261
```
57
262
58
- See documentation for [ Jakarta EE Bean Validation] ( https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/package-summary ) to learn more about validation annotations.
263
+ ``` Java
264
+ @Command (name = " login" )
265
+ public class LoginCommand {
59
266
60
- ## Handling validation errors
267
+ @Execute
268
+ void login (@Arg @Size (min = 8 ) String password ) {
269
+ // Command implementation
270
+ }
61
271
62
- If the validation fails, then the handler for ` JakartaResult ` will be called.
272
+ }
273
+ ```
63
274
64
275
``` Java
65
- public class JakartaResultHandler implements ResultHandler<SENDER , JakartaResult > {
276
+ @Command (name = " email" )
277
+ public class EmailCommand {
66
278
67
- @Override
68
- public void handle (Invocation<SENDER > invocation , JakartaResult result , ResultHandlerChain<SENDER > chain ) {
69
- for (ConstraintViolation<Object > violation : result. getViolations()) {
70
- String message = violation. getMessage();
71
- String messageKey = violation. getMessageTemplate();
72
-
73
- // ... (send message to the sender)
74
- }
279
+ @Execute
280
+ void email (@Arg @Email String email ) {
281
+ // Command implementation
75
282
}
76
283
77
284
}
78
285
```
79
286
80
- And register it in the ` LiteCommands ` builder:
81
-
82
287
``` Java
83
- .result(new JakartaResultHandler ())
84
- ```
288
+ @Command (name = " credit-card" )
289
+
290
+ public class CreditCommand {
291
+
292
+ @Execute
293
+ void credit (@Arg @CreditCardNumber String cardNumber ) {
294
+ // Command implementation
295
+ }
296
+
297
+ }
298
+ ```
299
+
300
+ See also:
301
+ - [ Jakarta EE Bean Validation annotations] ( https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/package-summary )
302
+ - [ Hibernate Validator] ( https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/ )
0 commit comments