Open Hirurg103 opened 4 years ago
Hi @Hirurg103, I'm not associated with the simple_command project, but I gave this a look.
I am not seeing this problem; I tried looking through the code and it appears to act the way you expect it to work. I wrote an rspec test to verify this behaviour, and it works as expected.
I created a new Rails, wrote a Command that recreates the errors in your example, and it still behaved as expected.
Have you found this issue to be resolved? or have I missed something while looking into it?
I have missed something, not including ActiveModel::Validations
does produce the errors object you are referencing.
I wrote a small gist that replicates this problem:
# app/commands/authenticate_user.rb
class AuthenticateUser
prepend SimpleCommand
# include ActiveModel::Validations
def initialize(email, password)
@email = email
@password = password
end
def call
errors.add(:email, "is invalid")
errors.add(:email, "is from a blocked domain")
errors.add(:password, "is too common")
nil
end
end
# app/controllers/authenticate_controller.rb
class AuthenticateController < ApplicationController
def create
load_users = AuthenticateUser.(params[:email], params[:password])
if load_users.success?
render json: load_users.result
else
render json: { errors: load_users.errors }
end
end
end
Output:
"errors" => {"email"=>"is from a blocked domain", "password"=>"is too common"}
Hi @bmorrall , yes, this error is reproducible with pure SimpleCommand
(without including ActiveModel::Validations
)
This PR should fix that https://github.com/nebulab/simple_command/pull/25
I had the same issue, thanks for reporting and for the PR, hope they will merge it soon and release a new version. The bump from 0.0.9 would be a MAJOR release since we had break changes and not a minor how it was.
After update to
v0.1.0
fromv0.0.9
tests started to fail withIn the controller errors are rendered the following way:
The problem is that
SimpleCommand::Errors#to_json
format differs fromActiveModel::Errors#to_json
It would be good to have
SimpleCommand::Errors#as_json
to be the same format asActiveModel::Errors#as_json