Request Parameters validations in vertx 4.0.3

1.3k views Asked by At

I was using vertx-Web-api-contracts in vertx 3.0 to validate my query parameters and form parameters using

HTTPRequestValidationHandler and ParameterTypeValidator

code sample :

private final ParameterTypeValidator fileIdValueValidator = new IdTypeValidator().create();
private final ParameterTypeValidator tokenTypeValidator = new TokenTypeValidator().create();

private HTTPRequestValidationHandler getDeleteRequestValidations() {
    final HTTPRequestValidationHandler validator = HTTPRequestValidationHandler.create()
        .addQueryParamWithCustomTypeValidator(PARAM__ID, IdValueValidator, true, false)
        .addHeaderParamWithCustomTypeValidator(HEADER_TOKEN, tokenTypeValidator, true, false);
    return validator;
}

Here I am using ParameterTypeValidator to provide custom logic to validate my parameters value. I am using customTypeValidator, because I need custom value validation for parameter value for ex. I need to validate my ID Parameter should contain four parts separated by _ ( part1_part2_part3_part4 )

Now while I am trying to migrate to vertx 4.0 and vertx-Web-api-contracts is deprecated, I am forced to use vertx-web-validation, but web-validation lacks custompropertyvalidation like api-contract-validations.

How I am trying to do,

private ValidationHandler getDeleteRequestValidations(final SchemaParser parser) {
    ValidationHandler validationHandler=ValidationHandler.builder(parser)
      .queryParameter(Parameters.param(PARAM__ID, stringSchema()))
      .headerParameter(Parameters.param(HEADER_TOKEN,stringSchema()))
      .build();
    return validationHandler;
  }

But I want to know how to pass/build a custom schema instead of stringSchema() or intSchema() to validate with custom logic. Earlier I was creating Custom classes and implements ParameterTypeValidator to provide custom validation logic(like IdTypeValidator()).

Is there any way to achieve same with web-validation.

1

There are 1 answers

0
mohamnag On

Not really familiar with the new validation mechanism, but will try to help you using some facts.

The new validation uses Json Schema, so you should first go for understanding that one: https://vertx.io/docs/vertx-json-schema/java/

Then, examining the Parameters.param() signature, the second parameter is just an instance of the io.vertx.json.schema.common.dsl.SchemaBuilder which is then described how to be built in this part of the docs:

https://vertx.io/docs/vertx-json-schema/java/#_building_your_schemas_from_code

I hope this will give you a direction on how to solve your problem.