Use Runtime Values in Schemas
mainSchema values can be proc objects that are resolved when the schema is rendered. This allows for dynamic schemas (e.g., enums populated from a database).
- Proc with no arguments: Evaluated in the instance context; can access instance variables.
- Proc with one argument: Receives the schema instance as an argument.
class RoleSchema < Schematist::Schema
description -> { "Roles available to #{@account.name}" }
string :role, enum: -> { @account.roles.pluck(:name) }
def initialize(account:)
super()
@account = account
end
end
RoleSchema.new(account: account).to_json_schema