module Amber::Schema::ControllerIntegration

Overview

Mixin for controllers to add schema validation

Direct including types

Defined in:

amber/schema/controller_integration_simple.cr

Constant Summary

SCHEMA_FORMAT_BY_CONTENT_TYPE = {"application/json" => :json, "application/cbor" => :cbor, "application/cose" => :cose}
SCHEMA_FORMAT_PREFERENCE = {:json => 0, :cbor => 1, :cose => 2}

Among types the client names outright with equal quality, the higher wins.

SCHEMA_RESPONSE_CONTENT_TYPES = ["application/json", "application/cbor", "application/cose"]

The content types #respond_with can write when no response schema narrows them.

Macro Summary

Instance Method Summary

Macro Detail

macro auto_validate #

Macro for auto validation (simplified)


[View source]
macro response_schema(action, schema_class = nil, status = 200, description = "Successful response", &block) #

Response schemas use the same contract and are enforced before a response is serialized.


[View source]
macro schema(action, schema_class = nil, &block) #

Bind a reusable schema class to a controller action:

schema :create, CreateUserSchema

Or define an action-local schema with the same field DSL:

schema :create do field :email, String, required: true, format: "email" end


[View source]
macro validate_schema(action = nil, required = true) #

Before filter to validate requests


[View source]

Instance Method Detail

def render_validated(data : Hash(String, JSON::Any), status : Int32 = 200) #

Render with response validation (backward compatible)


[View source]
def respond_with(data : NamedTuple, status : Int32 = 200, schema_name : String | Nil = nil, format : Symbol = :auto, key_provider : Amber::Schema::COSE::KeyProvider | Nil = Amber::Schema::COSE.key_provider) #

Respond with a NamedTuple, such as respond_with({title: "latest", count: 1}).

The tuple has Symbol keys and may hold any JSON-serializable value (Int32, arrays, nested tuples), so it goes through its own JSON form to become the Hash(String, JSON::Any) that response validation reads.


[View source]
def respond_with(data : Hash(String, JSON::Any), status : Int32 = 200, schema_name : String | Nil = nil, format : Symbol = :auto, key_provider : Amber::Schema::COSE::KeyProvider | Nil = Amber::Schema::COSE.key_provider) #

Respond with schema-validated data in the format the client accepts (JSON, CBOR or COSE).


[View source]
def respond_with(data : Hash, status : Int32 = 200, schema_name : String | Nil = nil, format : Symbol = :auto, key_provider : Amber::Schema::COSE::KeyProvider | Nil = Amber::Schema::COSE.key_provider) #

Respond with any JSON-serializable Hash, such as Hash(String, String) or Hash(Symbol, Int32). Its keys become strings and its values become JSON::Any, the same way to_json would write them.


[View source]
def respond_with(data : Nil = nil, status : Int32 = 200, schema_name : String | Nil = nil, format : Symbol = :auto, key_provider : Amber::Schema::COSE::KeyProvider | Nil = Amber::Schema::COSE.key_provider) #

Respond with an empty object.


[View source]
def respond_with_error(message : String, status : Int32 = 400, code : String | Nil = nil) #

Helper to create error response


[View source]
def respond_with_errors(errors : Array(Amber::Schema::Error), status : Int32 = 422) #

Helper to respond with validation errors


[View source]
def run_schema_validation(action : Symbol) : Nil #

Called from the controller callback pipeline before user callbacks.


[View source]
def validate_request(schema_name : String | Nil = nil) : Amber::Schema::LegacyResult #

Validate request against schema


[View source]
def validate_response(data : Hash(String, JSON::Any), status : Int32 = 200, schema_name : String | Nil = nil) : Amber::Schema::LegacyResult #

Validate response against schema


[View source]
def validated_as(type : T.class) : T forall T #

Recover the concrete schema type and its generated typed getters.


[View source]
def validated_params : Hash(String, JSON::Any) | Nil #

Helper to access validated data (alias for request_data)


[View source]
def validation_failed? : Bool #

Helper to check if validation passed


[View source]