Skip to content

Commit c23d4b0

Browse files
javadoc, support configuration over hocon config
1 parent baf422e commit c23d4b0

4 files changed

Lines changed: 65 additions & 12 deletions

File tree

modules/jooby-apt/src/main/java/io/jooby/apt/JoobyProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ private void verifyBeanValidationDependency(Collection<MvcRouter> routers) {
385385
"Unable to load 'BeanValidator' class. " +
386386
"Bean validation usage (@Valid) was detected, but the appropriate dependency is missing. " +
387387
"Please ensure that you have added the corresponding validation dependency " +
388-
"(e.g., jooby-hbv).");
388+
"(e.g., jooby-hibernate-validator).");
389389
}
390390
}
391391
}

modules/jooby-hibernate-validator/src/main/java/io/jooby/hibernate/validator/HibernateValidatorModule.java

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package io.jooby.hibernate.validator;
77

8+
import com.typesafe.config.Config;
89
import edu.umd.cs.findbugs.annotations.NonNull;
910
import io.jooby.Extension;
1011
import io.jooby.Jooby;
@@ -22,8 +23,36 @@
2223

2324
import static jakarta.validation.Validation.byProvider;
2425

26+
/**
27+
* Hibernate Validator Module: https://jooby.io/modules/hibernate-validator.
28+
*
29+
* <pre>{@code
30+
* {
31+
* install(new HibernateValidatorModule());
32+
*
33+
* }
34+
*
35+
* public class Controller {
36+
*
37+
* @POST("/create")
38+
* public void create(@Valid Bean bean) {
39+
* }
40+
*
41+
* }
42+
* }</pre>
43+
*
44+
* <p>Supports validation of a single bean, list, array, or map.</p>
45+
*
46+
* <p>The module also provides a built-in error handler that catches {@link ConstraintViolationException}
47+
* and transforms it into a {@link io.jooby.validation.ValidationResult}</p>
48+
*
49+
* @author kliushnichenko
50+
* @since 3.2.10
51+
*/
2552
public class HibernateValidatorModule implements Extension {
2653

54+
private static final String CONFIG_ROOT_PATH = "hibernate.validator";
55+
2756
private Consumer<HibernateValidatorConfiguration> configurer;
2857
private StatusCode statusCode = StatusCode.UNPROCESSABLE_ENTITY;
2958
private String title = "Validation failed";
@@ -79,14 +108,21 @@ public HibernateValidatorModule disableViolationHandler() {
79108
}
80109

81110
@Override
82-
public void install(@NonNull Jooby app) {
83-
HibernateValidatorConfiguration cfg = byProvider(HibernateValidator.class).configure();
111+
public void install(@NonNull Jooby app) throws Exception {
112+
Config config = app.getConfig();
113+
HibernateValidatorConfiguration hbvConfig = byProvider(HibernateValidator.class).configure();
114+
115+
if (config.hasPath(CONFIG_ROOT_PATH)) {
116+
config.getConfig(CONFIG_ROOT_PATH)
117+
.root()
118+
.forEach((k, v) -> hbvConfig.addProperty(CONFIG_ROOT_PATH + "." + k, v.unwrapped().toString()));
119+
}
84120

85121
if (configurer != null) {
86-
configurer.accept(cfg);
122+
configurer.accept(hbvConfig);
87123
}
88124

89-
try (ValidatorFactory factory = cfg.buildValidatorFactory()) {
125+
try (ValidatorFactory factory = hbvConfig.buildValidatorFactory()) {
90126
Validator validator = factory.getValidator();
91127
app.getServices().put(Validator.class, validator);
92128
app.getServices().put(MvcValidator.class, new MvcValidatorImpl(validator));
@@ -97,7 +133,7 @@ public void install(@NonNull Jooby app) {
97133
}
98134
}
99135

100-
static class MvcValidatorImpl implements MvcValidator<ConstraintViolationException> {
136+
static class MvcValidatorImpl implements MvcValidator {
101137

102138
private final Validator validator;
103139

@@ -106,7 +142,7 @@ static class MvcValidatorImpl implements MvcValidator<ConstraintViolationExcepti
106142
}
107143

108144
@Override
109-
public void validate(Object bean) {
145+
public void validate(Object bean) throws ConstraintViolationException {
110146
Set<ConstraintViolation<Object>> violations = validator.validate(bean);
111147
if (!violations.isEmpty()) {
112148
throw new ConstraintViolationException(violations);

modules/jooby-validation/src/main/java/io/jooby/validation/BeanValidator.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@
1212
import java.util.Collection;
1313
import java.util.Map;
1414

15+
/**
16+
* This class is a helper that provides a single entry point for bean validation without being strictly tied
17+
* to a specific bean validation implementation. Initially, it is utilized in `jooby-apt` for MVC bean validation,
18+
* but it can also be used independently in the scripting API.
19+
* It relies on an implementation of {@link MvcValidator} that should be available in the service registry
20+
*/
1521
public final class BeanValidator {
1622

1723
public static <T> T validate(Context ctx, T bean) {
18-
MvcValidator<?> validator = ctx.require(MvcValidator.class);
24+
MvcValidator validator = ctx.require(MvcValidator.class);
1925

2026
if (bean instanceof Collection<?>) {
2127
validateCollection(validator, (Collection<?>) bean);
@@ -30,13 +36,13 @@ public static <T> T validate(Context ctx, T bean) {
3036
return bean;
3137
}
3238

33-
private static void validateCollection(MvcValidator<?> validator, Collection<?> beans) {
39+
private static void validateCollection(MvcValidator validator, Collection<?> beans) {
3440
for (Object item : beans) {
3541
validateObject(validator, item);
3642
}
3743
}
3844

39-
private static void validateObject(MvcValidator<?> validator, Object bean) {
45+
private static void validateObject(MvcValidator validator, Object bean) {
4046
try {
4147
validator.validate(bean);
4248
} catch (Throwable e) {
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
package io.jooby.validation;
22

3-
public interface MvcValidator<E extends Throwable> {
3+
/**
4+
* This interface should be implemented by modules that provide bean validation functionality.
5+
* An instance of this interface must be registered in the Jooby service registry.
6+
* Doing so will enable bean validation for MVC routes.
7+
* For an example implementation, refer to the HibernateValidatorModule.
8+
*/
9+
public interface MvcValidator {
410

5-
void validate(Object bean) throws E;
11+
/**
12+
* Method should validate the bean and throw an exception if any constraint violations are detected
13+
* @param bean bean to be validated
14+
* @throws RuntimeException an exception with violations to be thrown (e.g. ConstraintViolationException)
15+
*/
16+
void validate(Object bean) throws RuntimeException;
617
}

0 commit comments

Comments
 (0)