module
Amber::Schema::ControllerIntegration
Overview
Mixin for controllers to add schema validation
Direct including types
Defined in:
amber/schema/controller_integration_simple.crMacro 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 : 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
-
#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)
Helper method to respond with schema-validated data
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)