module

Fm::Generable

Include this module in a struct/class to auto-generate JSON Schema for structured output with Session#respond_structured.

The type must also include JSON::Serializable.

struct Person
  include JSON::Serializable
  include Fm::Generable

  getter name : String
  getter age : Int32
end

schema = Person.json_schema
# => {"type" => "object", "properties" => {"name" => {"type" => "string"}, ...}, "required" => [...]}

You can use Fm::Guide annotations to add constraints:

struct Movie
  include JSON::Serializable
  include Fm::Generable

  getter title : String

  @[Fm::Guide(any_of: ["G", "PG", "PG-13", "R"])]
  getter rating : String

  @[Fm::Guide(minimum: 1, maximum: 10)]
  getter score : Int32

  @[Fm::Guide(pattern: "^[A-Z]")]
  getter director : String

  @[Fm::Guide(min_items: 1, max_items: 5)]
  getter genres : Array(String)
end

Class methods

type_to_schema(type : T.class) : JSON::Any forall T

Maps Crystal types to JSON Schema type descriptors.

Source