module Amber::Schema::ControllerIntegration

Overview

Mixin for controllers to add schema validation

Direct including types

Defined in:

amber/schema/controller_integration_simple.cr

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 : Hash(String, JSON::Any) | NamedTuple | 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) #

Helper method to respond with schema-validated data


[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]