Skip to content

Commit 7b40c9a

Browse files
committed
valid
1 parent 5766c9c commit 7b40c9a

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.avaje.http.api;
2+
3+
import static java.lang.annotation.ElementType.METHOD;
4+
import static java.lang.annotation.ElementType.PARAMETER;
5+
import static java.lang.annotation.ElementType.TYPE;
6+
import static java.lang.annotation.RetentionPolicy.SOURCE;
7+
8+
import java.lang.annotation.Retention;
9+
import java.lang.annotation.Target;
10+
11+
/**
12+
*
13+
* Add @Valid annotation on a controller/method/BeanParam that we want bean validation to be included for.
14+
* When we do this controller methods that take a request payload will then have the request bean
15+
* (populated by JSON payload or form parameters) validated before it is passed to the controller
16+
* method.
17+
*
18+
* When trying to validate a @BeanParam bean, this will need to be placed on the BeanParam type
19+
20+
*/
21+
@Retention(SOURCE)
22+
@Target({METHOD, TYPE, PARAMETER})
23+
public @interface Valid {}

http-generator-core/src/main/java/io/avaje/http/generator/core/ControllerReader.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ private boolean initDocHidden() {
162162
private boolean initHasValid() {
163163

164164
return findAnnotation(JavaxValidPrism::getOptionalOn).isPresent()
165+
|| findAnnotation(JakartaValidPrism::getOptionalOn).isPresent()
165166
|| findAnnotation(ValidPrism::getOptionalOn).isPresent();
166167
}
167168

http-generator-core/src/main/java/io/avaje/http/generator/core/ElementReader.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ private boolean useValidation() {
129129
}
130130
final var elementType = typeElement(rawType);
131131
return elementType != null
132-
&& (ValidPrism.isPresent(elementType) || JavaxValidPrism.isPresent(elementType));
132+
&& (ValidPrism.isPresent(elementType)
133+
|| JavaxValidPrism.isPresent(elementType)
134+
|| JakartaValidPrism.isPresent(elementType));
133135
}
134136

135137
private void readAnnotations(Element element, ParamType defaultType) {

http-generator-core/src/main/java/io/avaje/http/generator/core/package-info.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
@GeneratePrism(value = io.avaje.http.api.OpenAPIResponse.class, publicAccess = true)
2727
@GeneratePrism(value = io.avaje.http.api.OpenAPIResponses.class, publicAccess = true)
2828
@GeneratePrism(value = io.swagger.v3.oas.annotations.Hidden.class, publicAccess = true)
29-
@GeneratePrism(value = javax.validation.Valid.class, name = "JavaxValidPrism", publicAccess = true)
30-
@GeneratePrism(value = jakarta.validation.Valid.class, publicAccess = true)
3129
@GeneratePrism(value = org.jetbrains.annotations.NotNull.class, publicAccess = true)
3230
@GeneratePrism(value = io.avaje.http.api.Client.class, publicAccess = true)
3331
@GeneratePrism(value = io.avaje.http.api.Client.Import.class, publicAccess = true)
3432
@GeneratePrism(value = io.avaje.http.api.RequestTimeout.class, publicAccess = true)
33+
@GeneratePrism(value = javax.validation.Valid.class, name = "JavaxValidPrism")
34+
@GeneratePrism(value = jakarta.validation.Valid.class, name = "JakartaValidPrism")
35+
@GeneratePrism(value = io.avaje.http.api.Valid.class)
3536
package io.avaje.http.generator.core;
3637

3738
import io.avaje.prism.GeneratePrism;

tests/test-javalin-jsonb/src/main/java/org/example/myapp/web/HelloController.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import java.util.concurrent.CompletableFuture;
99
import java.util.concurrent.Executors;
1010

11-
import javax.validation.Valid;
12-
1311
import org.example.myapp.service.MyService;
1412

1513
import io.avaje.http.api.BeanParam;
@@ -23,6 +21,7 @@
2321
import io.avaje.http.api.Post;
2422
import io.avaje.http.api.Produces;
2523
import io.avaje.http.api.QueryParam;
24+
import io.avaje.http.api.Valid;
2625
import io.javalin.http.Context;
2726
import io.swagger.v3.oas.annotations.Hidden;
2827
import jakarta.inject.Inject;

0 commit comments

Comments
 (0)