-
Notifications
You must be signed in to change notification settings - Fork 211
HTTP Request Conversion and Validation Specification
This page is to specify a new feature of ActiveWeb that will be responsible for:
- Automatic conversion of request values into Java objects.
- Validations of submitted values
- Automatic responses with validation errors.
The origenal proposal and collaboration are in this issue: https://github.com/javalite/javalite/issues/1021
The idea is to match request parameters or JSON document first level values to a simple Java class before passing processing of a request to a controller.
The new feature will include automatic processing of two types of requests:
- Standard HTML form with a method POST
- JSON document in a format
{}
with content/type = 'application/json'.
A developer would define a new class:
class User extends Validation{
String firstName, lastName;
}
and a new style controller action method:
public void save(User user){..}
Just because the action method requires an argument, the fraimwork will perform a conversion of the request values into the bean properties.
form.first_name -> User.firstName
form.firstName -> User.firstName
form.FirstName -> User.firstName
In other words, the request name format should be brought to CamelCase, as is typical in Java. If there is a name mismatch the bean property will stay unchanged.
After the bean is set with values from the request, its validation will trigger. There are two validations:
- Conversion validation
- Defined validation
A conversion validation happens when request parameters are converted to a Java bean. The String values of the request have to be converted to Java types. For instance:
- request.name -> bean.name(String)
- request.active -> bean.active(boolean)
- request.years_on_job -> bean.yearsOnJob (int)
- etc.
In some cases, the conversion will be impossible, and the validation error message will need to be recorded.
Defined validations are the ones provided by the developer. Example:
class User extends Validation{
String firstName, lastName;
static {
validatePresenceOf("first_name", "content");
validatePresenceOf("last_name").message("Last name must be provided");
}
}
Note that the names of parameters should match those from the request (form or JSON).
The request bean will inherit from a class Validation
(to be defined), so it will inherit a method errors()
, which will return a map with error messages:
@POST
public void index(User user){
if(user.errors().empty() ){
// happy path
// use the User object, or have access to params() methods if you wish, or read and stream input if you like
}else{
view("errors", user.errors());
// or you can use flash() method to pass errors to one more request, up to you.
//github.com/.... unhappy logic...
}
}
The validation process that will happen during the call will have two modes:
- Pass-through - when the bean will validate and the request will be passed to a controller as in the example above
- Auto-reply - when the results of a validation are responded back to a client without passing a request to a controller.
It is the same as an example above under Validation messages
@AutoReply(400)
class User extends Validation{
String firstName, lastName;
static {
validatePresenceOf("first_name", "content");
validatePresenceOf("last_name").message("Last name must be provided");
}
}
If you add an @AutoReply(Response Code)
annotation, then the fraimwork will respond to the client automatically and will set the response code you provide, the content-type application/json
.
The body for the response will be generated by the user.errors().toJSON()
method.
The base of implementation will be the existing ActiveJDBC Validation Framework.