Form data validation in Vertx - feature request

763 views
Skip to first unread message

Manish Kumar

unread,
Jun 27, 2017, 11:07:25 AM6/27/17
to vert.x
Hey Guys,

We use Vertx as HTTP proxy as well as expose some REST API via same HTTP process.
I was wondering if we should have some way of form data(POST) validation in Vertx (similar to validation in Spring controller).

Of course form data validation can be done in request handler implementation, but I was thinking of more "separation of concerns".

Let me know if you guys think it will be worth to have form data validation built in (via some hook/handler).
I can help contribute if it's worth a feature.

~Manish


Manish Kumar

unread,
Jun 27, 2017, 11:12:28 AM6/27/17
to vert.x
Of course user will have to use BodyHandler so that all of form data is available as JSON(etc) and validation can be done. This is what I am thinking.


1 - During route creation, user define that they are using BodyHandler.
2 - During defining route request handler, user also provide the class (java bean) to which form data will be bind. Java bean should implement a Validator interface (similar to Spring's interface) and define all the validation in implemented "validate" method.
3 - During the request execution, Vertx will validate that the  form data is matching all the constraint.
4 - Return 400 or execute the request handler.

Thomas SEGISMONT

unread,
Jun 27, 2017, 11:50:24 AM6/27/17
to ve...@googlegroups.com
You can create a separate handler for body validation. In such a handler, you would proceed to next handler if validation succeeds, fail the request otherwise.

router.post().handler(BodyHandler.create());
router.post().handler(new MyValidatorHandler());

class MyValidatorHandler implements Handler<RoutingContext> {
  void handle(RoutingContext rc) {
    // get the body
    // if not null validate json or mapped pojo with your favorite framework
  }
}

And you get the separation of concerns.



--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/a3138dde-a17f-4eba-9b88-a98f14b6f707%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Manish Kumar

unread,
Jun 27, 2017, 4:10:38 PM6/27/17
to vert.x
We are doing something similar right now. I was wondering if anything more can be done. This itself seems like a clean enough implementation though. Thanks a lot for your response!!!
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.

Manish Kumar

unread,
Jun 27, 2017, 4:12:53 PM6/27/17
to vert.x
Since we use different validation for different endpoint - this is what we are doing.

router.post().handler(BodyHandler.create());

router.post("/abc").handler(new MyAbcValidator());
router.post("/abc").handler(new MyAbcHandler());

router.post("/xyz").handler(new MyXyzValidator());
router.post("/xyz").handler(new MyXyzHandler());

Paulo Lopes

unread,
Jun 28, 2017, 8:51:25 AM6/28/17
to vert.x
Hi,

You should follow this year GSoC project:

https://github.com/slinkydeveloper/vertx-web/tree/designdriven

It solves what you're looking for, given that you have your API input/output defined in OpenAPI format it will perform the validation for you.

Please consider review the work and test ;)

Cheers,

Paulo

Manish Kumar

unread,
Mar 29, 2018, 2:55:36 PM3/29/18
to vert.x
@paulo - Link seems to be broken.

Paulo Lopes

unread,
Mar 29, 2018, 3:42:47 PM3/29/18
to vert.x
Hi Manish,

This project has graduated and lives now as a tech preview under the official vert-x3 org. You can read the current release docs here:

https://vertx.io/docs/vertx-web-api-contract/java/

and the code is here:

https://github.com/vert-x3/vertx-web/tree/master/vertx-web-api-contract
Reply all
Reply to author
Forward
0 new messages