Consider this snippet from the "Getting Started" guide:
module Web::Controllers::Books
class Create
include Web::Action
expose :book
params do
param :book do
param :title, presence: true
param :author, presence: true
end
end
def call(params)
if params.valid?
@book = BookRepository.create(Book.new(params[:book]))
redirect_to '/books'
end
end
end
end
Notice the validations on title
and author
, which live here in the controller action. My question is: Why are these validations on the action params rather than on the Book
entity? That is, assuming the validations were on Book
, you could write something like:
def call(params)
book = Book.new(params)
if book.valid?
@book = BookRepository.create(Book.new(params[:book]))
redirect_to '/books'
end
end
and just get rid of the params
block altogether. This seems more natural to me, and would promote easier re-use of the validations across different actions.
Are there advantages to the params
method that I'm not seeing? Are there drawbacks to placing the validations on the Book
entity?