Why Contracts
The Problem
# Rails controller
def create
@user = User.new(user_params)
# What is user_params?
# What fields are allowed?
# What validations apply?
end
private
def user_params
params.require(:user).permit(:name, :email, :age)
# Scattered across the file
# Not type-safe
# Validation elsewhere
endThe Contract Solution
API as Interface
Request Contract = API Input
Response Contract = API Output
Validation Colocation
Type-Safe Access
Self-Documenting Code
Versioning
Testing Benefits
Code Generation
Comparison
Aspect
Implicit (params)
Explicit (contracts)
Trade-offs
More Boilerplate
Rigid Structure
The Azu Philosophy
See Also
Last updated
Was this helpful?
