Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.

Commit 06cf3c8

Browse files
committed
Update Jakarta documentation
1 parent e13a050 commit 06cf3c8

File tree

4 files changed

+243
-23
lines changed

4 files changed

+243
-23
lines changed

.github/workflows/ build.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ env:
1717
ARTIFACT: webHelpLITECOMMANDS2-all.zip
1818
# Docker image version
1919
DOCKER_VERSION: 241.15989
20-
ALGOLIA_ARTIFACT: 'algolia-indexes-HI.zip'
20+
ALGOLIA_ARTIFACT: 'algolia-indexes-LITECOMMANDS.zip'
2121
ALGOLIA_APP_NAME: 'AA5N7A7U42'
2222
ALGOLIA_INDEX_NAME: 'litecommands'
2323
ALGOLIA_KEY: '${{ secrets.ALGOLIA_KEY }}'
@@ -48,6 +48,7 @@ jobs:
4848
path: |
4949
artifacts/${{ env.ARTIFACT }}
5050
artifacts/report.json
51+
artifacts/${{ env.ALGOLIA_ARTIFACT }}
5152
retention-days: 7
5253

5354
test:

Writerside/cfg/buildprofiles.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<algolia-id>AA5N7A7U42</algolia-id>
1111
<algolia-index>litecommands</algolia-index>
1212
<algolia-api-key>b36a06ddbc4e036fb9dffc0ed94e22ac</algolia-api-key>
13+
<web-root>docs.rollczi.dev</web-root>
1314
</variables>
1415
</build-profile>
1516

393 KB
Loading
Lines changed: 240 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# Jakarta Extension
22

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:
45

56
```Java
67
@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+
}
811
```
912

10-
## Dependency
13+
## 0. Dependency
1114
Add the following dependency to your project:
1215

1316
<tabs>
@@ -30,15 +33,217 @@ implementation("dev.rollczi:litecommands-adventure:%latest_version%")
3033
</tabs>
3134

3235

33-
## Registering the extension
36+
## 1. Register Jakarta extension
3437

3538
Register the extension in the `LiteCommands` builder:
3639

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+
3753
```java
3854
.extension(new LiteJakartaExtension<>())
3955
```
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+
```
4090

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
42247

43248
```Java
44249
@Command(name = "ban")
@@ -47,38 +252,51 @@ public class BanCommand {
47252
@Execute
48253
void ban(
49254
@Arg @Pattern(regexp = "[a-zA-Z_]+") String name,
50-
@Arg @Future Instant date,
255+
@Arg @Future Instant date
51256
) {
52257
// Command implementation
53258
}
54259

55260
}
56261
```
57262

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 {
59266

60-
## Handling validation errors
267+
@Execute
268+
void login(@Arg @Size(min = 8) String password) {
269+
// Command implementation
270+
}
61271

62-
If the validation fails, then the handler for `JakartaResult` will be called.
272+
}
273+
```
63274

64275
```Java
65-
public class JakartaResultHandler implements ResultHandler<SENDER, JakartaResult> {
276+
@Command(name = "email")
277+
public class EmailCommand {
66278

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
75282
}
76283

77284
}
78285
```
79286

80-
And register it in the `LiteCommands` builder:
81-
82287
```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

Comments
 (0)