use of io.vertx.ext.web.api.RequestParameter in project vertx-web by vert-x3.
the class OpenAPI3ValidationTest method testDefaultIntQueryParameter.
@Test
public void testDefaultIntQueryParameter() throws Exception {
Operation op = testSpec.getPaths().get("/queryTests/defaultInt").getGet();
OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
loadHandlers("/queryTests/defaultInt", HttpMethod.GET, false, validationHandler, (routingContext) -> {
RequestParameters params = routingContext.get("parsedParameters");
RequestParameter requestParameter = params.queryParameter("parameter");
assertTrue(requestParameter.isInteger());
routingContext.response().setStatusMessage(requestParameter.toString()).end();
});
testRequest(HttpMethod.GET, "/queryTests/defaultInt", 200, "1");
}
use of io.vertx.ext.web.api.RequestParameter in project vertx-web by vert-x3.
the class OpenAPI3ValidationTest method testFormArrayParameter.
@Test
public void testFormArrayParameter() throws Exception {
Operation op = testSpec.getPaths().get("/formTests/arraytest").getPost();
if (op.getParameters() == null)
op.setParameters(new ArrayList<>());
OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
loadHandlers("/formTests/arraytest", HttpMethod.POST, false, validationHandler, (routingContext) -> {
RequestParameters params = routingContext.get("parsedParameters");
List<String> result = new ArrayList<>();
for (RequestParameter r : params.formParameter("values").getArray()) result.add(r.getInteger().toString());
routingContext.response().setStatusMessage(params.formParameter("id").getString() + serializeInCSVStringArray(result)).end();
});
String id = "anId";
List<String> valuesArray = new ArrayList<>();
for (int i = 0; i < 4; i++) valuesArray.add(getSuccessSample(ParameterType.INT).getInteger().toString());
String values = serializeInCSVStringArray(valuesArray);
MultiMap form = MultiMap.caseInsensitiveMultiMap();
form.add("id", id);
form.add("values", values);
testRequestWithForm(HttpMethod.POST, "/formTests/arraytest", FormType.FORM_URLENCODED, form, 200, id + values);
}
use of io.vertx.ext.web.api.RequestParameter in project vertx-web by vert-x3.
the class ValidationExamples method example3.
public void example3(RoutingContext routingContext) {
RequestParameters params = routingContext.get("parsedParameters");
RequestParameter awesomeParameter = params.queryParameter("awesomeParameter");
if (awesomeParameter != null) {
if (!awesomeParameter.isEmpty()) {
// Parameter exists and isn't empty
// ParameterTypeValidator mapped the parameter in equivalent language object
Integer awesome = awesomeParameter.getInteger();
} else {
// Parameter exists, but it's empty
}
} else {
// Parameter doesn't exist (it's not required)
}
}
use of io.vertx.ext.web.api.RequestParameter in project vertx-web by vert-x3.
the class ExpandedObjectFieldValidator method isValid.
/**
* Function that checks if parameter is valid. It returns a RequestParameter object that will be linked inside
* {@link RequestParameters}. For more info, check {@link RequestParameter}.
*
* @param value value of parameter to test
* @return request parameter value
*/
@Override
public RequestParameter isValid(String value) throws ValidationException {
RequestParameter requestParameter = innerValidator.isValid(value);
requestParameter.setName(fieldName);
Map<String, RequestParameter> map = new HashMap<>();
map.put(fieldName, requestParameter);
return new RequestParameterImpl(objectName, map);
}
use of io.vertx.ext.web.api.RequestParameter in project vertx-web by vert-x3.
the class ObjectTypeValidator method validate.
@Override
protected RequestParameter validate(Map<String, String> values) throws ValidationException {
Map<String, RequestParameter> parsedParams = new HashMap<>();
for (Map.Entry<String, ObjectField> field : fieldsMap.entrySet()) {
if (!values.containsKey(field.getKey())) {
if (field.getValue().required)
throw ValidationException.ValidationExceptionFactory.generateObjectFieldNotFound(field.getKey());
else if (field.getValue().validator.getDefault() != null)
parsedParams.put(field.getKey(), RequestParameter.create(field.getKey(), field.getValue().validator.getDefault()));
} else {
RequestParameter param = field.getValue().validator.isValid(values.get(field.getKey()));
param.setName(field.getKey());
parsedParams.put(field.getKey(), param);
}
}
return RequestParameter.create(parsedParams);
}
Aggregations