package examples;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.*;
import io.vertx.ext.web.api.RequestParameter;
import io.vertx.ext.web.api.RequestParameters;
import io.vertx.ext.web.handler.*;
import io.vertx.ext.web.api.validation.HTTPRequestValidationHandler;
import io.vertx.ext.web.api.validation.ParameterType;
import io.vertx.ext.web.api.validation.ValidationException;
public class ValidationExamples {
public void example1(Vertx vertx, Router router) {
HTTPRequestValidationHandler validationHandler = HTTPRequestValidationHandler.create().addQueryParam("parameterName", ParameterType.INT, true).addFormParamWithPattern("formParameterName", "a{4}", true).addPathParam("pathParam", ParameterType.FLOAT);
}
public void example2(Vertx vertx, Router router, HTTPRequestValidationHandler validationHandler) {
router.route().handler(BodyHandler.create());
router.get("/awesome/:pathParam")
.handler(validationHandler)
.handler((routingContext) -> {
RequestParameters params = routingContext.get("parsedParameters");
Integer parameterName = params.queryParameter("parameterName").getInteger();
String formParameterName = params.formParameter("formParameterName").getString();
Float pathParam = params.pathParameter("pathParam").getFloat();
})
.failureHandler((routingContext) -> {
Throwable failure = routingContext.failure();
if (failure instanceof ValidationException) {
String validationErrorMessage = failure.getMessage();
}
});
}
public void example3(RoutingContext routingContext) {
RequestParameters params = routingContext.get("parsedParameters");
RequestParameter awesomeParameter = params.queryParameter("awesomeParameter");
if (awesomeParameter != null) {
if (!awesomeParameter.isEmpty()) {
Integer awesome = awesomeParameter.getInteger();
} else {
}
} else {
}
}
public void example4(RequestParameters params) {
RequestParameter body = params.body();
if (body != null) {
JsonObject jsonBody = body.getJsonObject();
}
}
public void example5(Vertx vertx, Router router, HTTPRequestValidationHandler validationHandler) {
router.get("/awesome/:pathParam")
.handler(validationHandler)
.handler((routingContext) -> {
})
.failureHandler((routingContext) -> {
Throwable failure = routingContext.failure();
if (failure instanceof ValidationException) {
String validationErrorMessage = failure.getMessage();
}
});
router.errorHandler(400, routingContext -> {
if (routingContext.failure() instanceof ValidationException) {
String validationErrorMessage = routingContext.failure().getMessage();
} else {
routingContext.response().setStatusCode(400).end();
}
});
}
}