Skip to content

Commit dfbfd0a

Browse files
committed
validation: handler/filter for lambda routes + remove MvcValidator
- Remove `MvcValidator`. Merge content into `BeanValidator` - Rename BeanValidator.validate to BeanValidator.apply (avoid method name conflict) - Creates BeanValidator.validate() route handler/filter which intercept calls to HTTP request object and run validator - TODO: if we agreed on merging of MvcValidator we can remove/get rid of `jooby-validation` module
1 parent 29d8cb3 commit dfbfd0a

File tree

27 files changed

+1070
-167
lines changed

27 files changed

+1070
-167
lines changed

docs/asciidoc/modules/avaje-validator.adoc

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ import io.jooby.avaje.validator.AvajeValidatorModule;
6464
import io.jooby.avaje.validator.AvajeValidatorModule
6565
6666
{
67-
install(new AvajeValidatorModule())
67+
install(AvajeValidatorModule())
6868
}
6969
----
7070

@@ -139,17 +139,15 @@ class Controller {
139139

140140
4) Usage in in script/lambda routes
141141

142-
Jooby doesn't provide fully native bean validation in script/lambda at the moment,
143-
but you can use a helper that we utilize under the hood in MVC routes:
144-
145142
.Java
146143
[source, java, role="primary"]
147144
----
148145
import io.jooby.validation.BeanValidator;
149146
150147
{
148+
use(BeanValidator.validate());
151149
post("/validate", ctx -> {
152-
Bean bean = BeanValidator.validate(ctx, ctx.body(Bean.class));
150+
Bean bean = ctx.body(Bean.class);
153151
...
154152
});
155153
}
@@ -161,15 +159,31 @@ import io.jooby.validation.BeanValidator;
161159
import io.jooby.validation.BeanValidator
162160
163161
{
162+
use(BeanValidator.validate())
164163
post("/validate") {
165-
val bean = BeanValidator.validate(ctx, ctx.body(Bean.class))
164+
val bean = ctx, ctx.body(Bean.class)
166165
...
167166
}
168167
}
169168
----
170169

171170
`BeanValidator.validate()` behaves identically to validation in MVC routes.
172-
It also supports validating list, array, and map of beans
171+
It also supports validating list, array, and map of beans.
172+
173+
There is a handler version of it, so you can apply per route:
174+
175+
.validate
176+
[source, java]
177+
----
178+
import io.jooby.validation.BeanValidator.validate;
179+
180+
{
181+
post("/validate", validate(ctx -> {
182+
Bean bean = ctx.body(Bean.class);
183+
...
184+
}));
185+
}
186+
----
173187

174188
=== Constraint Violations Rendering
175189

docs/asciidoc/modules/hibernate-validator.adoc

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import io.jooby.hibernate.validator.HibernateValidatorModule;
2727
import io.jooby.hibernate.validator.HibernateValidatorModule
2828
2929
{
30-
install(new HibernateValidatorModule())
30+
install(HibernateValidatorModule())
3131
}
3232
----
3333

@@ -102,17 +102,15 @@ class Controller {
102102

103103
4) Usage in in script/lambda routes
104104

105-
Jooby doesn't provide fully native bean validation in script/lambda at the moment,
106-
but you can use a helper that we utilize under the hood in MVC routes:
107-
108105
.Java
109106
[source, java, role="primary"]
110107
----
111108
import io.jooby.validation.BeanValidator;
112109
113110
{
111+
use(BeanValidator.validate());
114112
post("/validate", ctx -> {
115-
Bean bean = BeanValidator.validate(ctx, ctx.body(Bean.class));
113+
Bean bean = ctx.body(Bean.class);
116114
...
117115
});
118116
}
@@ -124,8 +122,9 @@ import io.jooby.validation.BeanValidator;
124122
import io.jooby.validation.BeanValidator
125123
126124
{
125+
use(BeanValidator.validate())
127126
post("/validate") {
128-
val bean = BeanValidator.validate(ctx, ctx.body(Bean.class))
127+
val bean = ctx, ctx.body(Bean.class)
129128
...
130129
}
131130
}
@@ -134,6 +133,21 @@ import io.jooby.validation.BeanValidator
134133
`BeanValidator.validate()` behaves identically to validation in MVC routes.
135134
It also supports validating list, array, and map of beans
136135

136+
There is a handler version of it, so you can apply per route:
137+
138+
.validate
139+
[source, java]
140+
----
141+
import io.jooby.validation.BeanValidator.validate;
142+
143+
{
144+
post("/validate", validate(ctx -> {
145+
Bean bean = ctx.body(Bean.class);
146+
...
147+
}));
148+
}
149+
----
150+
137151
=== Constraint Violations Rendering
138152

139153
`HibernateValidatorModule` provides default built-in error handler that

0 commit comments

Comments
 (0)