use of io.vertx.ext.web.api.validation.ParameterTypeValidator in project vertx-web by vert-x3.
the class StringTypeValidatorTest method isNotValidMaxLength.
@Test(expected = ValidationException.class)
public void isNotValidMaxLength() {
ParameterTypeValidator validator = new StringTypeValidator(null, null, 3, null);
validator.isValid("aaaa");
}
use of io.vertx.ext.web.api.validation.ParameterTypeValidator in project vertx-web by vert-x3.
the class StringTypeValidatorTest method isNotValidMinLength.
@Test(expected = ValidationException.class)
public void isNotValidMinLength() {
ParameterTypeValidator validator = new StringTypeValidator(null, 2, null, null);
validator.isValid("a");
}
use of io.vertx.ext.web.api.validation.ParameterTypeValidator in project vertx-examples by vert-x3.
the class ValidationExampleServer method start.
@Override
public void start() throws Exception {
Router router = Router.router(vertx);
// If you want to validate bodies, don't miss that handler!
router.route().handler(BodyHandler.create());
// Create Validation Handler with some stuff
HTTPRequestValidationHandler validationHandler = HTTPRequestValidationHandler.create().addQueryParam("parameterName", ParameterType.INT, true).addFormParamWithPattern("formParameterName", "a{4}", true).addPathParam("pathParam", ParameterType.FLOAT);
router.post("/hello").handler(validationHandler).handler((routingContext) -> {
// To consume parameters you can get it in the "classic way" of Vert.x Web
// or you can use the RequestParameters that contains parameters already parsed (and maybe transformed)
// Get RequestParameters container
RequestParameters params = routingContext.get("parsedParameters");
// Get parameters
Integer parameterName = params.queryParameter("parameterName").getInteger();
String formParameterName = params.formParameter("formParameterName").getString();
Float pathParam = params.pathParameter("pathParam").getFloat();
// Do awesome things with your parameters!
}).failureHandler((routingContext) -> {
Throwable failure = routingContext.failure();
if (failure instanceof ValidationException) {
// Something went wrong during validation!
String validationErrorMessage = failure.getMessage();
routingContext.response().setStatusCode(400).end();
}
});
// A very basic example of JSON body validation
router.post("/jsonUploader").handler(HTTPRequestValidationHandler.create().addJsonBodySchema("{type: string}")).handler((routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
JsonObject body = params.body().getJsonObject();
}));
// Write your own parameter type validator
ParameterTypeValidator primeCustomTypeValidator = value -> {
try {
int number = Integer.parseInt(value);
// Yeah I know this is a lazy way to check if number is prime :)
for (int i = 2; i < number; i++) {
if (// Number is not prime
number % i == 0)
throw ValidationException.ValidationExceptionFactory.generateNotMatchValidationException("Number is not prime!");
}
// We pass directly the parsed parameter
return RequestParameter.create(number);
} catch (NumberFormatException e) {
throw ValidationException.ValidationExceptionFactory.generateNotMatchValidationException("Wrong integer format");
}
};
// Now we create the route and mount the HTTPRequestValidationHandler
router.get("/superAwesomeParameter").handler(HTTPRequestValidationHandler.create().addQueryParamWithCustomTypeValidator("primeNumber", primeCustomTypeValidator, true, false)).handler((routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
Integer primeNumber = params.queryParameter("primeNumber").getInteger();
}));
vertx.createHttpServer().requestHandler(router::accept).listen();
}
use of io.vertx.ext.web.api.validation.ParameterTypeValidator in project vertx-web by vert-x3.
the class SingleValueParameterTypeValidatorTest method defaultTest.
@Test
public void defaultTest() {
ParameterTypeValidator validator = new MockSingleValueParameterTypeValidator("hello");
assertEquals(RequestParameter.create("hello"), validator.isValid(null));
assertEquals(RequestParameter.create("world"), validator.isValid("world"));
}
Aggregations