Closed Travisnv closed 3 years ago
reference docs at: https://avaje.io/http/#bean-validation
The docs are not super clear but there are 3 steps:
@Valid
to the controller (this is probably the step I suspect you are missing?)@Valid
and validation annotations to the request bean being postedCan you check if you have those 3 steps?
HelloController has the @Valid
on it
https://github.com/avaje/avaje-http/blob/master/tests/test-javalin/src/main/java/org/example/myapp/web/HelloController.java#L31
HelloForm as @Valid
plus the validation annotations
https://github.com/avaje/avaje-http/blob/master/tests/test-javalin/src/main/java/org/example/myapp/web/HelloForm.java#L12
The generated code for that route becomes:
ApiBuilder.post("/hello/saveform", ctx -> {
ctx.status(201);
HelloForm helloForm = new HelloForm(
ctx.formParam("name"),
ctx.formParam("email")
);
helloForm.url = ctx.formParam("url");
helloForm.startDate = toLocalDate(ctx.formParam("startDate"));
validator.validate(helloForm);
controller.saveForm(helloForm);
});
... and noting that the ...$Route
now has a dependency on Validator
which is passed in on the constructor. So for HelloController$Route
we see ...
@Generated("io.dinject.javalin-generator")
@Singleton
public class HelloController$Route implements WebRoutes {
private final HelloController controller;
private final Validator validator;
public HelloController$Route(HelloController controller, Validator validator) {
this.controller = controller;
this.validator = validator;
}
@Override
public void registerRoutes() {
...
@BeanParam
on @Get
annotated controller method.generated code for @Get
don't have validator part while @Put
and @Post
do have.
@Get don't have validator part while @Put and @Post do have.
Right, so a bug specific to @Get
then - cool.
Hi @rbygrave,
I wonder can we have validation annotation for bean param? I have a look at generated code for javalin and don't see any part for validation despite of having annotation on bean param class.
Thanks, Travis