Skip to content

class Athena::Framework::RequestBodyConverter
inherits Athena::Framework::ParamConverter #

Deserializes the request body into an object, runs any validations defined on it, then provides the object to the controller action. Uses the type restriction of the related controller action argument to know what type to deserialize into. Supports both ASR::Serializable and JSON::Serializable types.

require "athena"

# A type representing the structure of the request body.
struct UserCreate
  # Include some modules to tell Athena this type can be deserialized
  # via the Serializer component and validated via the Valdiator component.
  include AVD::Validatable
  include ASR::Serializable

  # Assert the user's name is not blank.
  @[Assert::NotBlank]
  getter first_name : String

  # Assert the user's name is not blank.
  @[Assert::NotBlank]
  getter last_name : String

  # Assert the user's email is not blank and is valid.
  @[Assert::NotBlank]
  @[Assert::Email(:html5)]
  getter email : String
end

class UserController < ATH::Controller
  @[ATHA::Post("/user")]
  @[ATHA::View(status: :created)]
  @[ATHA::ParamConverter("user_create", converter: ATH::RequestBodyConverter)]
  def new_user(user_create : UserCreate) : UserCreate
    # Use the provided UserCreate instance to create an actual User DB record.
    # For purposes of this example, just return the instance.

    user_create
  end
end

ATH.run

Making a request to the /user endpoint with the following payload:

{
  "first_name": "George",
  "last_name": "",
  "email": "dietrich.app"
}

Would return the response:

{
  "code": 422,
  "message": "Validation failed",
  "errors": [
    {
      "property": "last_name",
      "message": "This value should not be blank.",
      "code": "0d0c3254-3642-4cb0-9882-46ee5918e6e3"
    },
    {
      "property": "email",
      "message": "This value is not a valid email address.",
      "code": "ad9d877d-9ad1-4dd7-b77b-e419934e5910"
    }
  ]
}

While a valid request would return:

{
  "first_name": "George",
  "last_name": "Dietrich",
  "email": "george@dietrich.app"
}

Constructors#

Methods#

#apply(request : ATH::Request, configuration : Configuration(ArgType)) : Nil forall ArgType #

:inherit:

View source