Request

Request contracts define the expected shape of incoming request data with validation.

Including Request

struct MyRequest
  include Azu::Request

  getter field1 : String
  getter field2 : Int32?

  def initialize(@field1 = "", @field2 = nil)
  end
end

Validation Macros

validate

Add validation rules to a field.

validate field_name, rule: value, ...

Available Rules:

Rule
Description
Example

presence

Field must not be empty

presence: true

length

String length constraints

length: {min: 2, max: 100}

format

Regex pattern match

format: /@/

numericality

Numeric constraints

numericality: {greater_than: 0}

inclusion

Value in set

inclusion: {in: ["a", "b"]}

exclusion

Value not in set

exclusion: {in: ["admin"]}

presence

Validate field is not empty/nil.

length

Validate string length.

Options:

  • min : Int32 - Minimum length

  • max : Int32 - Maximum length

  • is : Int32 - Exact length

format

Validate against regular expression.

numericality

Validate numeric values.

Options:

  • greater_than : Number

  • greater_than_or_equal_to : Number

  • less_than : Number

  • less_than_or_equal_to : Number

  • equal_to : Number

inclusion

Validate value is in allowed set.

exclusion

Validate value is not in forbidden set.

Instance Methods

valid?

Check if request passes all validations.

Returns: Bool

errors

Get validation errors.

Returns: Array(Error)

validate

Override to add custom validation logic.

Error Class

Azu::Request::Error

Represents a validation error.

Properties:

  • field : Symbol - Field name

  • message : String - Error message

EmptyRequest

Use for endpoints that don't accept body data.

Request Parsing

Requests are automatically parsed from:

  • JSON body (application/json)

  • Form data (application/x-www-form-urlencoded)

  • Multipart form (multipart/form-data)

File Uploads

Handle file uploads with HTTP::FormData::File:

File Properties:

  • filename : String? - Original filename

  • body : IO - File content

  • headers : HTTP::Headers - File headers

Complete Example

See Also

Last updated

Was this helpful?