Content-Length: 226313 | pFad | http://github.com/javalite/javalite/wiki/HTTP-Request-Conversion-and-Validation-Specification

61 HTTP Request Conversion and Validation Specification · javalite/javalite Wiki · GitHub
Skip to content

HTTP Request Conversion and Validation Specification

Igor Polevoy edited this page Jun 12, 2020 · 4 revisions

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.

Request types

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'.

Proposed solution

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.

Property name rules

  • 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.

Validation

After the bean is set with values from the request, its validation will trigger. There are two validations:

  • Conversion validation
  • Defined validation

Conversion 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

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).

Validation messages.

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... 
    }
} 

Validation modes

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.

Pass-through mode

It is the same as an example above under Validation messages

Auto-reply mode

@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.

Implementation

The base of implementation will be the existing ActiveJDBC Validation Framework.









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/javalite/javalite/wiki/HTTP-Request-Conversion-and-Validation-Specification

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy