module
Amber::Schema::ControllerIntegration
Overview
Mixin for controllers to add schema validation
Direct including types
Defined in:
amber/schema/controller_integration_simple.crConstant 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_withcan write when no response schema narrows them.
Macro Summary
-
auto_validate
Macro for auto validation (simplified)
-
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.
-
schema(action, schema_class = nil, &block)
Bind a reusable schema class to a controller action:
-
validate_schema(action = nil, required = true)
Before filter to validate requests
Instance Method Summary
-
#render_validated(data : Hash(String, JSON::Any), status : Int32 = 200)
Render with response validation (backward compatible)
-
#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}). -
#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).
- #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(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.
-
#respond_with_error(message : String, status : Int32 = 400, code : String | Nil = nil)
Helper to create error response
-
#respond_with_errors(errors : Array(Amber::Schema::Error), status : Int32 = 422)
Helper to respond with validation errors
-
#run_schema_validation(action : Symbol) : Nil
Called from the controller callback pipeline before user callbacks.
-
#validate_request(schema_name : String | Nil = nil) : Amber::Schema::LegacyResult
Validate request against schema
-
#validate_response(data : Hash(String, JSON::Any), status : Int32 = 200, schema_name : String | Nil = nil) : Amber::Schema::LegacyResult
Validate response against schema
-
#validated_as(type : T.class) : T forall T
Recover the concrete schema type and its generated typed getters.
-
#validated_params : Hash(String, JSON::Any) | Nil
Helper to access validated data (alias for request_data)
-
#validation_failed? : Bool
Helper to check if validation passed
Macro Detail
Response schemas use the same contract and are enforced before a response is serialized.
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
Before filter to validate requests
Instance Method Detail
Render with response validation (backward compatible)
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.
Respond with schema-validated data in the format the client accepts (JSON, CBOR or COSE).
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.
Respond with an empty object.
Helper to create error response
Helper to respond with validation errors
Called from the controller callback pipeline before user callbacks.
Validate request against schema
Validate response against schema
Recover the concrete schema type and its generated typed getters.
Helper to access validated data (alias for request_data)