Skip to content

struct Athena::Framework::Controller::ValueResolvers::RequestBody
inherits Struct #

Attempts to resolve the value of any parameter with the ATHR::RequestBody::Extract annotation by deserializing the request body of the request into an object of the type of the related parameter. Also handles running any validations defined on it, if it is AVD::Validatable. Requires the related parameter's be either either the ASR::Serializable or JSON::Serializable module.

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 JSON::Serializable

  # Assert the user's name is not blank.
  getter first_name : String

  # Assert the user's name is not blank.
  getter last_name : String

  # Assert the user's email is not blank and is valid.
  getter email : String

class UserController < ATH::Controller
  @[ATHA::View(status: :created)]
  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.


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

  "first_name": "George",
  "last_name": "",
  "email": ""

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 this response body, with a 201 status code:

  "first_name": "George",
  "last_name": "Dietrich",
  "email": "[email protected]"

Included modules




#resolve(request : ATH::Request, parameter : ATH::Controller::ParameterMetadata(T)) : T | Nil forall T#


View source