Documentation for LLMs on building APIs with the Apia framework
Enums define a fixed set of allowed string values. They are used for both input arguments and output fields.
module MyAPI
module Enums
class ColorSchemeEnum < Apia::Enum
name "Color scheme"
description "The available color schemes for the user interface"
value "light", "Light mode"
value "dark", "Dark mode"
value "system", "Follow system preference"
end
end
end
value(name, description = nil)Adds an allowed value to the enum. The name is the string value, and the description is optional documentation.
# With description
value "pending", "The invite is pending"
value "accepted", "The invite has been accepted"
# Without description
value "light"
value "dark"
# Symbol values (converted to strings)
value :pending
value :accepted
value :rejected
value :expired
cast(&block)Defines custom logic to transform the value before returning it in a response:
class InviteStateEnum < Apia::Enum
value :pending
value :accepted
value :rejected
value :expired
cast do |value|
value.to_s
end
end
You can generate enum values from model constants or database records:
class TimeZoneEnum < Apia::Enum
TimeZone::VALID_IDENTIFIERS.each do |time_zone|
value time_zone
end
end
class ColorSchemeEnum < Apia::Enum
User::COLOR_SCHEMES.each do |scheme|
value scheme, scheme.capitalize
end
end
class SuspensionTypeEnum < Apia::Enum
Organization::SUSPENSION_TYPES.each_value do |type|
value type, type
end
end
argument :time_zone, Enums::TimeZoneEnum
argument :color_scheme, Enums::ColorSchemeEnum
field :state, Enums::InviteStateEnum
field :identity_check_state, Enums::IdentityCheckStateEnum
class Time < Apia::Object
field :day_of_week, Objects::DayEnum do
backend { |t| t.strftime('%A') }
end
end
When an enum is used as an argument type, Apia automatically validates that the provided value is one of the allowed values. If not, an InvalidArgumentError is raised with the issue invalid_enum_value.
class DayEnum < Apia::Enum
value "Sunday"
value "Monday"
value "Tuesday"
value "Wednesday"
value "Thursday"
value "Friday"
value "Saturday"
end
Enums can be placed either in the enums/ directory or alongside objects in the objects/ directory, depending on your project conventions.