Skip to content

Commit 23a7045

Browse files
committed
Enhance Javadoc documentation for TemplateStringFormatter and JavalidationModule
1 parent 39e4102 commit 23a7045

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

src/main/java/io/github/raniagus/javalidation/format/TemplateStringFormatter.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,52 @@
11
package io.github.raniagus.javalidation.format;
22

3+
/**
4+
* Strategy interface for formatting {@link TemplateString} instances into plain strings.
5+
* <p>
6+
* This functional interface allows pluggable formatting strategies for error messages.
7+
* Implementations typically use the template and arguments from {@link TemplateString} to
8+
* produce a formatted output string, potentially considering locale, resource bundles, or
9+
* other contextual information.
10+
* <p>
11+
* <b>Built-in implementations:</b>
12+
* <ul>
13+
* <li>{@link MessageFormatTemplateStringFormatter} - Uses Java's {@link java.text.MessageFormat}</li>
14+
* <li>{@code MessageSourceTemplateStringFormatter} - Uses Spring's {@code MessageSource} for i18n</li>
15+
* </ul>
16+
* <p>
17+
* <b>Usage example:</b>
18+
* <pre>{@code
19+
* TemplateStringFormatter formatter = TemplateStringFormatter.getDefault();
20+
* TemplateString template = new TemplateString("Age must be at least {0}", new Object[]{18});
21+
* String formatted = formatter.format(template); // "Age must be at least 18"
22+
* }</pre>
23+
* <p>
24+
* Custom formatters can be registered with Jackson via {@link io.github.raniagus.javalidation.jackson.JavalidationModule}
25+
* or Spring Boot auto-configuration properties.
26+
*
27+
* @see TemplateString
28+
* @see MessageFormatTemplateStringFormatter
29+
*/
330
@FunctionalInterface
431
public interface TemplateStringFormatter {
32+
/**
33+
* Formats a template string into a plain string.
34+
* <p>
35+
* Implementations should use the {@link TemplateString#message()} as the template
36+
* and {@link TemplateString#args()} as the placeholder arguments.
37+
*
38+
* @param templateString the template to format (must not be null)
39+
* @return the formatted string (never null)
40+
*/
541
String format(TemplateString templateString);
642

43+
/**
44+
* Returns the default formatter using {@link java.text.MessageFormat} syntax.
45+
* <p>
46+
* The default formatter supports placeholders like {@code {0}}, {@code {1}}, etc.
47+
*
48+
* @return a {@link MessageFormatTemplateStringFormatter} instance
49+
*/
750
static TemplateStringFormatter getDefault() {
851
return new MessageFormatTemplateStringFormatter();
952
}

src/main/java/io/github/raniagus/javalidation/jackson/JavalidationModule.java

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,41 @@
77
import tools.jackson.databind.ValueSerializer;
88
import tools.jackson.databind.module.SimpleModule;
99

10+
/**
11+
* Jackson module for serializing javalidation types ({@link TemplateString} and {@link ValidationErrors}).
12+
* <p>
13+
* This module registers custom serializers to control how validation errors are serialized to JSON.
14+
* By default, it uses:
15+
* <ul>
16+
* <li>{@link TemplateStringSerializer} for {@link TemplateString} - formats templates using {@link TemplateStringFormatter}</li>
17+
* <li>{@link ValidationErrorsMixIn} for {@link ValidationErrors} - structures errors as {@code {root: [...], fields: {...}}}</li>
18+
* </ul>
19+
* <p>
20+
* <b>Basic usage (default configuration):</b>
21+
* <pre>{@code
22+
* ObjectMapper mapper = JsonMapper.builder()
23+
* .addModule(JavalidationModule.getDefault())
24+
* .build();
25+
* }</pre>
26+
* <p>
27+
* <b>Custom formatter (for internationalization):</b>
28+
* <pre>{@code
29+
* JavalidationModule module = JavalidationModule.builder()
30+
* .withTemplateStringFormatter(myCustomFormatter)
31+
* .build();
32+
* }</pre>
33+
* <p>
34+
* <b>Flattened errors (flat array instead of nested structure):</b>
35+
* <pre>{@code
36+
* JavalidationModule module = JavalidationModule.builder()
37+
* .withFlattenedErrors()
38+
* .build();
39+
* }</pre>
40+
*
41+
* @see TemplateStringSerializer
42+
* @see FlattenedErrorsSerializer
43+
* @see ValidationErrorsMixIn
44+
*/
1045
public class JavalidationModule extends SimpleModule {
1146
private final boolean useDefaultSerializer;
1247

@@ -34,39 +69,115 @@ public void setupModule(SetupContext context) {
3469
}
3570
}
3671

72+
/**
73+
* Returns a module instance with default serializers.
74+
* <p>
75+
* Equivalent to {@code JavalidationModule.builder().build()}.
76+
*
77+
* @return a new module with default configuration
78+
*/
3779
public static JavalidationModule getDefault() {
3880
return builder().build();
3981
}
4082

83+
/**
84+
* Returns a new builder for customizing the module configuration.
85+
*
86+
* @return a new {@link Builder} instance
87+
*/
4188
public static Builder builder() {
4289
return new Builder();
4390
}
4491

92+
/**
93+
* Builder for configuring {@link JavalidationModule} with custom serializers.
94+
* <p>
95+
* Use this builder to customize how {@link TemplateString} and {@link ValidationErrors}
96+
* are serialized to JSON.
97+
*
98+
* @see #withTemplateStringFormatter(TemplateStringFormatter)
99+
* @see #withFlattenedErrors()
100+
*/
45101
public static class Builder {
46102
private ValueSerializer<TemplateString> templateStringSerializer = new TemplateStringSerializer();
47103
private @Nullable ValueSerializer<ValidationErrors> validationErrorsSerializer;
48104

105+
/**
106+
* Creates a new builder with default serializers.
107+
*/
49108
public Builder() {
50109
}
51110

111+
/**
112+
* Configures a custom formatter for {@link TemplateString} serialization.
113+
* <p>
114+
* This is a convenience method that wraps the formatter in a {@link TemplateStringSerializer}.
115+
* Use this to customize how template messages are formatted (e.g., for i18n).
116+
* <p>
117+
* Example:
118+
* <pre>{@code
119+
* builder.withTemplateStringFormatter(new MessageSourceTemplateStringFormatter(messageSource, locale))
120+
* }</pre>
121+
*
122+
* @param formatter the formatter to use (must not be null)
123+
* @return this builder for method chaining
124+
* @see #withTemplateStringSerializer(ValueSerializer)
125+
*/
52126
public Builder withTemplateStringFormatter(TemplateStringFormatter formatter) {
53127
return withTemplateStringSerializer(new TemplateStringSerializer(formatter));
54128
}
55129

130+
/**
131+
* Configures the module to serialize {@link ValidationErrors} as a flat array of error strings.
132+
* <p>
133+
* By default, errors are serialized with nested structure: {@code {root: [...], fields: {...}}}.
134+
* This method changes the serialization to a simple flat array: {@code ["error1", "error2", ...]}.
135+
* <p>
136+
* Example output:
137+
* <pre>{@code
138+
* ["Name is required", "Age must be positive"]
139+
* }</pre>
140+
*
141+
* @return this builder for method chaining
142+
* @see FlattenedErrorsSerializer
143+
*/
56144
public Builder withFlattenedErrors() {
57145
return withValidationErrorsSerializer(new FlattenedErrorsSerializer());
58146
}
59147

148+
/**
149+
* Configures a custom serializer for {@link TemplateString}.
150+
* <p>
151+
* Use this for full control over template serialization. For most use cases,
152+
* {@link #withTemplateStringFormatter(TemplateStringFormatter)} is simpler.
153+
*
154+
* @param templateStringSerializer the custom serializer (must not be null)
155+
* @return this builder for method chaining
156+
*/
60157
public Builder withTemplateStringSerializer(ValueSerializer<TemplateString> templateStringSerializer) {
61158
this.templateStringSerializer = templateStringSerializer;
62159
return this;
63160
}
64161

162+
/**
163+
* Configures a custom serializer for {@link ValidationErrors}.
164+
* <p>
165+
* Use this for full control over error structure serialization. For flattened output,
166+
* {@link #withFlattenedErrors()} is simpler.
167+
*
168+
* @param validationErrorsSerializer the custom serializer (must not be null)
169+
* @return this builder for method chaining
170+
*/
65171
public Builder withValidationErrorsSerializer(ValueSerializer<ValidationErrors> validationErrorsSerializer) {
66172
this.validationErrorsSerializer = validationErrorsSerializer;
67173
return this;
68174
}
69175

176+
/**
177+
* Builds the configured {@link JavalidationModule}.
178+
*
179+
* @return a new module instance with the configured serializers
180+
*/
70181
public JavalidationModule build() {
71182
return new JavalidationModule(templateStringSerializer, validationErrorsSerializer);
72183
}

0 commit comments

Comments
 (0)